I have two views a levelComplete view and a levelSelector view. What I would like to do is when the levelComplete shows or the ViewDidLoad
occurs on that view I would like to send a delegate to the level selector to show a button in the view and then make that button UserInteractionEnabled
so then I will then be able to programme that button to do something if its not hidden.
1 回答
You want to necessarily do it via a delegate. Coz you can do it in a simpler way as well. When you call your secondView, just tell your button to hide. So your modified code to calling the second view controller becomes:
-(IBAction)passdata:(id)sender {
secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];
self.secondviewData = second;
sender.hidden=YES;
secondviewData.passedValue = textfield.text;
[self presentModalViewController:second animated:YES];
}
And then you can set it to visible when your view loads up again using viewDidLoad. I can tell you how to do it via delegates if you need. Lemme know what works best.
EDIT - Solution by Delegates
Your secondView's Header file will be as follows:
@protocol SecondViewHandlerDelegate <NSObject>
- (void)viewHasBeenLoaded:(BOOL)success;
@end
@interface secondview :UIViewController {
IBOutlet UILabel *label;
NSString *passedValue;
}
@property (nonatomic, retain)NSString *passedValue;
-(IBAction)back:(id)sender;
@end
Then, in the implementation file of secondView(.m), synthesise the delegate first by @synthesize delegate;
. After this, in your viewDidLoad
of secondView, add the following line:
[[self delegate] viewHasBeenLoaded:YES];
That should be enough for your secondView. Now onto the firstViewController, perform the following steps:
In the header file (.h), import your second view and implement the protocol:
@interface ViewController :UIViewController <SecondViewHandlerDelegate>{
..
..
}
In the implementation file (.m) of your firstViewController, implement this method:
- (void)viewHasBeenLoaded:(BOOL)success
{
NSLog("Delegate Method Called");
[myButton setHidden:YES];
}
And finally, in your code when you call the secondView, add this line: secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil]; second.delegate = self; ...
That should solve your purpose. I'd appreciate if you could mark the answer as correct as well. Thanks :)