In my iPhone app, I have two UIViewcontroler class firstView and secondView.
From the firstView, I am presenting secondView using presentModalViewController:animated:
method.
Problem is that when I am dismissing secondView, I want to hide button in firstView.
Although It does execute the code in firstView [button setHidden:YES];
, but still it does not hide the button.
What could be wrong?
Hope you have declared property and synthesized the IBOutlet button
.
Make an object of FirstViewController in SecondViewController.h and property and synthesize it.
SecondViewController.h
@interface SecondViewController {
.
.
FirstViewController *firstView;
.
.
}
@property (nonatomic,strong) FirstViewController *firstView;
@end
SecondViewController.m
@implementation SecondViewController
.
.
@synthesize firstView;
.
.
@end
Now when you present a modal view from firstView
FirstViewController.m
-(IBAction)presentModalView {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondView.firstView = self;
[self presentModalViewController:secondView animated:YES];
}
Now in SecondViewController where you dismiss SecondViewController just add this code.
SecondViewController.m
-(IBAction)dismissModalView {
[self.firstView.button setHidden:YES];
[self dismissModalViewControllerAnimated:YES];
}
EDIT:
Refer to this link:
@protocol implementation in @interface in Objective-C
EDIT-2: With Protocol implementation
SecondViewController.h
@protocol SecondViewControllerDelegate <NSObject>
@required
- (void)hideButton;
@end
@interface SecondViewController {
.
.
id <SecondViewControllerDelegate> delegate;
.
.
}
@property (retain) id delegate;
@end
SecondViewController.m
@implementation SecondViewController
.
.
@synthesize delegate;
.
.
@end
Now when you present a modal view from firstView
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController<SecondViewControllerDelegate>
{
.
.
.
.
}
.
.
@end
FirstViewController.m
-(IBAction)presentModalView {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondView.delegate = self;
[self presentModalViewController:secondView animated:YES];
}
#pragma mark - SecondViewController Delegate
- (void)hideButton
{
[self.button setHidden:YES]; //Here button is UIButton you want to hide when second view is dismissed.
}
Now in SecondViewController where you dismiss SecondViewController just add this code.
SecondViewController.m
-(IBAction)dismissModalView {
[delegate hideButton];
[self dismissModalViewControllerAnimated:YES];
}
Let me know if you need more help on this.
Hope this helps.