I’m developing a simple app with multiple views.
I have a base view and a second view, called after the base.
The base view serves as a delegate for the second view.
I would like, in my second view, to set and retrieve the value of a property in the baseView (the ‘name’).
Since, in the secondView, the delegate is a secondViewDelegate object, it doesn’t have a ‘name’ method.
baseView.h (skipping imports):
@interface baseView : UIViewController <secondViewDelegate> {
}
@property (retain) NSString *name;
@end
secondView.h (skipping imports):
@protocol secondViewDelegate;
@interface secondView : UIViewController{
}
@property (nonatomic,assign) id <secondViewDelegate> delegate;
@end
@protocol secondViewDelegate
- (void) secondViewDidFinish:(secondView *)controller;
@end
What is the usual approach here?
Do I really need to implement a getter and a setter for the protocol? Like:
@protocol secondViewDelegate
- (void) secondViewDidFinish:(secondView *)controller;
- (NSString *)getName;
- (void)setName:(NSString *)newName;
@end
Or is there any good way to actually access the baseView property, or at least get the instance? (Is it a good idea?)
I’m still pretty new to iOS development, in case it isn’t obvious.
Since you go to the trouble of defining a delegate protocol (a good thing IMO), you should stick to your design decision and add the property to the protocol instead of looking for a “clever” solution that makes the warning go away. You can use
@propertyfor the protocol declaration, like this:If it doesn’ feel good to have the property in your protocol, then you should probably review your design decisions: What is
secondViewDelegatesupposed to do, or why doessecondViewneed to set the property.