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
MainViewControllerand 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:
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:Moreover, the
MainViewControllerhas 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:Once you have all this stuff you will be able to change the button’s image this way:
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.Then, your
MainViewControllerwill have to implement this delegate:Now, in the definition of the
PasswordViewControlleryou will have to define a property calleddelegate:When you create the new instance of
PasswordViewControllerfrom theMainViewControlleryou should set theMainViewControlleras its delegate:and when you change the password in the
PasswordViewControlleryou simply will have to call the delegate’s method. It’s as easy as this: