简体   繁体   English

iPhone:从外部根控制器切换视图

[英]iPhone: Switching Views From Outside Root Controller

I am using a UINavigationController to switch between views. 我正在使用UINavigationController在视图之间切换。 What I would like is for each view to have the ability to control when it is swapped out for another view by having buttons within the view. 我希望每个视图都能够通过在视图内设置按钮来控制何时将其换出到另一个视图。 All of the samples I've seen thus far have placed buttons on a toolbar, which is located on the root view containing the Switch View Controller rather than the views, them self. 到目前为止,我所看到的所有示例都在工具栏上放置了按钮,该工具栏位于包含Switch View Controller(而不是视图)的根视图上,它们本身就是它们。 Is it possible to do what I want? 可以做我想做的吗? I can't figure how to wire up the connection back to the UINavigationController. 我不知道如何将连接重新连接到UINavigationController。

I'm having a difficult time wording this, so please feel free to let me know if you need additional clarification. 我在说这句话时很困难,所以如果您需要进一步的说明,请随时告诉我。

Read about delegates. 了解有关代表的信息。 Delegates are a common method to signal stuff from objects to their "parents" or any other objects. 委托是一种从对象向其“父级”或任何其他对象发出信号的常用方法。

You should have a "delegate" property (can really be called anything, this is just a convention) on your child views. 您的子视图上应该有一个“ delegate”属性(实际上可以称为任何东西,这只是一个约定)。 You can have buttons in your child views. 您可以在子视图中具有按钮。

You declare the delegate like this: 您可以这样声明委托:

interface ChildView : UIViewController {
    id delegate;
}

@property (assign) id delegate;

implementation ChildView

@synthesize delegate;

Then, when you set up your child views inside your UINavigationController, you do: 然后,当您在UINavigationController中设置子视图时,您将执行以下操作:

ChildView *childView = [[ChildView alloc] init...]
childView.delegate = self;

Inside your child view, you have a button method: 在子视图中,您有一个按钮方法:

- (IBAction) didPressButton:(id)sender {
    [self.delegate didPressButtonToSwapView];
}

Inside your UINavigationController, you have a method: 在UINavigationController内部,您有一个方法:

- (void) didPressButtonToSwapView {
    [self popViewController]; // use the right names, I made these up :)
    [self pushAnotherViewController];
}

You should also read about protocols which would make the above code more robust and would help you make sure you only call the right methods on delegate, but I did not want to complicate this example. 您还应该阅读一些协议 ,这些协议可以使上面的代码更健壮,并有助于确保仅在委托上调用正确的方法,但我不想使此示例复杂化。

EDIT: yes, the cleanest way to get rid of the warning is to use a protocol. 编辑:是的,摆脱警告的最干净的方法是使用协议。 Just put this in a separate .h file: 只需将其放在单独的.h文件中:

@protocol SwitchingDelegate
- (void) didPressButtonToSwapView;
@end

Include this .h in the UINavController header, and say the UINavController implements the protocol: 在UINavController标头中包含此.h,并说UINavController实现该协议:

@interface MyNav: UINavController <SwitchingDelegate> { ...

Implement the method in the implementation (you don't need anything more in the interface). 在实现中实现该方法(界面中不再需要任何其他功能)。

In your ChildView, say that the delegate must implement the protocol: change all the declarations to: 在您的ChildView中,说该委托必须实现协议:将所有声明更改为:

id<SwitchingDelegate> delegate;

The compiler then helps you by checking whether the delegate objects really implement the protocol. 然后,编译器通过检查委托对象是否真正实现了协议来为您提供帮助。 You should not get any warnings when you have completed all of this correctly. 正确完成所有这些操作后,您不应收到任何警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM