Manish Jain Manish Jain - 2 months ago 7x
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];



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

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" 

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

    NSString *textForTheButton = [[notification userInfo]objectForKey:@"newText"];
    [self.myButton setTitle:textForTheButton 

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