简体   繁体   中英

How can I change a button's image in a controller from another one?

I've got a small issue in setting the image for a button in MainViewController , from another UIViewController .

Actually what happens is this: when I click the lockButton in the MainViewController , it opens a different UIViewController where I can set password. Once the password is set successfully, I need to change the image of the lockButton in the MainViewController .

Can anyone please help me to get out of this issue? Thank you.

You have to point to the instance of the MainViewController and then change the settings for its inner components. First of all, you should be able to access that instance from your app delegate, at least this is straightforward if you're using the default project template from Xcode. To do this you should use this snippet:

MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
MainViewController *view = delegate.mainViewController;

To ensure this is working, check if the MainViewController 's instance is available as a property in the app delegate's header file. You should find something like this:

@property(readonly) MainViewController *mainViewController;

Moreover, the MainViewController has to expose as a property the button you are looking for, through its header file. If you are designing the view via IB, you should have something like this, I suppose:

@property(nonatomic, retain) IBOutlet UIButton *lockButton;

Once you have all this stuff you will be able to change the button's image this way:

MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
MainViewController *view = delegate.mainViewController;
UIImage *image = [UIImage imageNamed:@"image.png"];
[view.lockButton setImage:btnImage forState:UIControlStateNormal];

This should work fine for you. You'd probably have to adapt something here or there, but this is the way you should implement the solution.

Edit

This solution will work fine but it's not the only one. A different approach could be the one of using protocols. What you'll do is creating a way to make the controller that saves the password talk back to the main view controller.

Your PasswordViewController (let's call the new view controller this way) should define in the header file a new protocol. Eg.

@protocol PasswordViewControllerDelegate
    @required
    - (void) passwordChanged;
@end

Then, your MainViewController will have to implement this delegate:

@interface MainViewController : UIViewController <PasswordViewControllerDelegate>
    ....
@end

@implementation MainViewController
    ....

    - (void) passwordChanged {
        [lockButton setImage:[UIImage imageNamed:@"myImage.png" forState:UIControlStateNormal];
    }

    ....
}

Now, in the definition of the PasswordViewController you will have to define a property called delegate :

@property (nonatomic, retain) id<PasswordViewControllerDelegate> delegate;

When you create the new instance of PasswordViewController from the MainViewController you should set the MainViewController as its delegate:

PasswordViewController pvc = [[PasswordViewController alloc] init];
pvc.delegate = self;
// then you present the view controller

and when you change the password in the PasswordViewController you simply will have to call the delegate's method. It's as easy as this:

[self.delegate passwordChanged];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM