开发者

What is the best way to switch views with UIViewController and UIViews without interface builder?

Hey guys ive thought of a couple solutions but not really happy with them so need some ideas please.

I have:

  • 3 UIView instances view1, view2, view3
  • 1 UIViewController instance, UIViewController1 that stores view1,view2 and view3

I want:

  • to put a method in view1 that will开发者_C百科 tell UIViewController1 to switch to view2.

Do I need to put a pass a reference to UIViewController1 in view1? would this be the only correct way?


First off, are these full screen views? If so you should consider having 3 view controllers, one for each view and perhaps installing them in a UITabBarController, which is similar to the behavior you described. But in any case:

No, a view should not tell a controller to do anything.

In an MVC architecture, views present information and receive (some) user interaction. It is the controller's responsibility to decide what to do with that action.

What the view does do is tell the controller that an interaction has occurred.

So the main question for you is - what event would be generating this behavior?

Is it a button? Buttons (subclasses of UIControl) use a target-action pattern to handle this communication. Your view should provide a reference to the button:

@property (nonatomic, retain) UIButton *switcherButton;

Next, in the view controller you would implement a method:

-(void)viewSwitcherButtonTapped:(id)sender;

This is how your view can tell the controller that a UI event occurred. That's all it should do. It is up to the controller to decide, based on state information in the Model, or based on its own internal state or logic what should happen when that button is tapped.

And finally, after the view is created, the controller would hook itself up to the button:

[view.switcherButton addTarget:self action:@selector(viewSwitcherButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

This way any view controller can use this view and the view does not depend on a reference to the controller (as it shouldn't).

But the button is just one example and your implementation might be different if the event that generates the behavior is different. Answer that question and maybe I can amend this answer to apply.

You should take a look at

"The Model-View-Controller Design Pattern", "The Target-Action Mechanism" and "Delegation" in http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html


You have 2 options:

the quick and dirty one: just declare a property in each view that points to the UIViewController, define it as assign to avoid circular references (i presume you retain the views in your viewcontroller). then you can call the methods on uiviewcontroller via this property

the nice one: have each view broadcast a notification, the uiviewcontroller registers for this notification and in its callback does what it needs to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜