Manish Jain Manish Jain - 3 months ago 25
iOS Question

Update UIButton title from other view...?

I am developing an iPad app. At some stage I need to use dropdown type functionality. So, I'm using UIPopoverView for the same.

When IBAction fire on tap of particular UIButton, I adjust popoverview rendering UITableViewController.

And all thing working fine. I need when user tap any of the cell, related cell value need to set in attached UIButton title.

enter image description here

Here, popover view is the UITableViewController view, which I create separately. And call it on select Outlet IBAction.

CGRect dropdownPosition = CGRectMake(self.btnOutlet.frame.origin.x, self.btnOutlet.frame.origin.y, self.btnOutlet.frame.size.width, self.btnOutlet.frame.size.height);
[pcDropdown presentPopoverFromRect:dropdownPosition inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];


Thanks

Answer

Sangony answer is almost correct, but with some minor changes, instead of register the method without parameters as observer, you should add it admitting one parameter:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(someAction:)
                                             name:@"ButtonNeedsUpdate"
                                           object:nil];

Then, when you post the notification(in your table's view didSelectRow:atIndexPath:), you can add an object(a NSDictionay) as user info:

//...
NSDictionary *userInfoDictionary = @{@"newText":@"some text"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonNeedsUpdate" 
                                                    object:self 
                                                  userInfo:userInfoDictionary];
//...

And then in the class that is observing for this notification, you can work with the data in the someAction action method like this:

-(void)someAction:(NSNotification)notification{
    NSString *textForTheButton = [[notification userInfo]objectForKey:@"newText"];
    [self.myButton setTitle:textForTheButton 
                   forState:UIControlStateNormal];
    //...
}

Your's button title now should be "some text".

Comments