0

I have a table in the 1st interface controller , when a press on a row , a modal interface controller opens up , it contains a button.

I want the button to delete the row in the first interface controller.

Here is my code :

In the first interface controller

Blockquote

   // It opens up a modal view ( with the context of the tapped row )

   override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? {
       var timelineRow = timeline.reverse()
       return timelineRow[rowIndex]

   }

Blockquote

And here is my code in the second interface controller

Blockquote

   override func awakeWithContext(context: AnyObject?) {
   super.awakeWithContext(context)

      sentContext = (context as? Dictionary)!
      sentRow = sentContext
      //sentRow contains the context 
   }


  @IBAction func deleteRow() {
     var sentRow : [String:String] = ["action":"delete"]   
     NSNotificationCenter.defaultCenter().postNotificationName("notification_DeleteRow", object: nil, userInfo: sentRow)    
     dismissController()
 }

Blockquote

  • I've sent the index of the row through the contextForSegueWithIdentifier.

  • In the 2nd Interface Controller I've extracted the Context and put it in variable

  • I then send back the userInfo throught the NSNotificationCenter

My Problem :

  • How can I use the userInfo sent back from the modal controller in order to delete the tapped row.

  • How would I manage to delete the tapped row (1st IC) by pressing on the delete button situated in the (2nd IC)

When you tap on a row, a modal view opens up This modal view is opened when you tap on a row

AziCode
  • 2,510
  • 6
  • 27
  • 53

2 Answers2

2

There are a few options in this situation:

  1. You could use NSUserDefaults, and while it would work, this isn't how that class is intended to be used.
  2. You can create your own custom NSNotification and broadcast it from the modal controller. Your first interface controller would listen for this event and delete the appropriate record.
  3. You can pass a reference to your first interface controller to the modal controller and retrieve it in awakeWithContext:. This allows you to set the first interface controller as a delegate. Once this happens, you can define whatever protocol you'd like to inform the first controller of important events.

I have a blog post that goes into more detail on the second two topics: Advanced WatchKit Interface Controller Techniques

Mike Swanson
  • 3,337
  • 1
  • 17
  • 15
  • - I have created an NSNotification in the 2nd Interface Controller ( inside the IBAction) , and it is broadcasting it to the 1st interface Controller. -So now I should create a function in the 1st Interface Controller to delete the Row, but How can I access that same Row and delete it ? – AziCode May 26 '15 at 18:58
  • 1
    You can pass your own information (like a record ID, for example) in the `userInfo` dictionary of your notification. In your first controller, you can look for that ID and perform the correct deletion. – Mike Swanson May 26 '15 at 19:34
  • Could you please give me more indication? should I create a dictionary in the 2nd interface Controller ? should I look for that ID in the (awakeWithContext of the 1st Interface Controller or in the function that is created through the NSNotification? – AziCode May 26 '15 at 21:17
  • It depends on your app. Since I only know what you originally posted, it's difficult to say. But, you can include a dictionary of any information you'd like when you broadcast the notification from the second controller and read it in the method that handles that notification in the first. From there, you can do whatever you need to do to accomplish your goal. – Mike Swanson May 27 '15 at 04:00
1

This can be achieved with custom delegate easily,

@protocol MyCustomDelegate <NSObject>

- (void)deleteButtonTapped:(id)sender;

@end

- (IBAction)deleteButtonTapped:(id)sender {
    if ([self.delegate respondsToSelector:@selector(deleteButtonTapped:)]) {
        [self.delegate deleteButtonTapped:sender];
    };
}

More detailed answer is here.

Community
  • 1
  • 1