I am using a UINavigationController to switch between views. 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. Is it possible to do what I want? I can't figure how to wire up the connection back to the UINavigationController.
我正在使用UINavigationController在视图之间切换。我想要的是每个视图都能够通过在视图中使用按钮来控制何时换出另一个视图。到目前为止我看到的所有样本都在工具栏上放置了按钮,工具栏位于包含Switch View Controller的根视图中,而不是视图,它们是self。有可能做我想要的吗?我无法想象如何将连接连接回UINavigationController。
I'm having a difficult time wording this, so please feel free to let me know if you need additional clarification.
我正在努力解决这个问题,所以如果您需要进一步澄清,请随时告诉我。
1 个解决方案
#1
2
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. 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.
然后,编译器会通过检查委托对象是否真正实现协议来帮助您。当您正确完成所有这些操作后,您不应该收到任何警告。
#1
2
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. 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.
然后,编译器会通过检查委托对象是否真正实现协议来帮助您。当您正确完成所有这些操作后,您不应该收到任何警告。