Basic concept: communicating between two views?
How do i send in开发者_如何转开发formation between two views (and hence, two classes)? Am I looking for my app delegate? Is there a better or alternative way?
If you want to send information back, you can use target-action (the way UIControl does), or you can send NSNotifications, or use a generic delegate protocol. Unless this is information of use throughout your application, putting it into your app delegate may be overkill.
I would use the Application Delegate. Or, if one view owns the other, you can initialize them together and keep the main reference to it in the class.
I always find it useful to have a global Context object to keep global information among views. This information could be, configuration information, device current orientation, database handlers, etc.
For the variables you need cross-access for, you can use Properties.
class VC1 : UIViewController {
NSString* v1;
NSString* v2;
}
@property (copy) NSString *v1;
@property (copy) NSString *v2;
And then, in the other view:
class VC2 : UIViewController {
VC1 *vc1;
}
And in you message implementations in VC2 you can use VC1's v1 and v2 like this:
- (void) someMessage {
NSLog(@"VC1's v1 value is %@ and v2 value is %@", [vc1 v1], [vc1 v2]);
}
Hope it helps.
精彩评论