从presentModalViewController取回时如何更新滑块的值并进行切换? [英] How to update the value of slider and switch when get back from presentModalViewController?
问题描述
我有一些具有某些设置属性的帐户.作为设置,有 UISlider
的值和 UISwitch
的值.当我运行应用程序时,它可以正常工作,因为viewDidLoad方法有效,所以我可以显示 NSUserDefaults
中的最后一个值.顺便说一下,我的应用程序有一个标签栏控制器.因此,当我切换选项卡时,它也可以正常工作,因为我可以获取switch和slider的值并在viewWillAppear方法中更新它们.但是在我的设置开关中,我显示了一个其中包含用户列表的视图,因此用户可以选择任何帐户.从显示的视图返回时,我无法更新switch和Slider的值.我需要一个触发器方法来更新它们的值.有什么办法吗?
I have accounts which they have some settings attributes. As settings there are value of UISlider
and value of UISwitch
. When i run the application it works fine i can display the last value from NSUserDefaults
because viewDidLoad method works. My application has a tab bar controller by the way. So when i switch tabs it works fine too because i can get the values of switch and slider and update them in viewWillAppear method. But in my setting switch, i present a view which there are user list in it, so the user can select any account. When i get back from presented view i can't update the values of switch and slider. I need a trigger method to update their values. Is there any way to do that?
推荐答案
是的,这是3种苹果方法:
Yes this are the 3 apple ways to do it:
1) Delegation
2) NSNotificationCenter
3) KVO
但是,对于您的特殊情况来说,模态视图是Apple推荐的一种委托模式,也是我推荐的一种.
However for your particular case a modal view, the delegation pattern is the one Apple recommends, and is the one I would recommend as well.
尽管如此,它需要比其他2个选项更多的编码.
It requires a bit more coding than the other 2 options though.
首先,您必须在模态显示的视图中声明协议:
First you have to declare the protocol in the view you are presenting modally:
in your .h
@protocol MyProtocol;
@interface MyClass : NSObject
@property (nonatomic, weak) id<MyProtocol> delegate;
@end
@protocol MyProtocol <NSObject>
-(void)updateValues:(NSArray *)values;
@end
然后在从原始视图控制器以模态形式呈现模态视图之前,只需像这样设置delgate
Then before presenting the modal view modally from the original view controller simply set the delgate like this
myModallyPresentedController.delegate = self;
然后让您的演示视图控制器采用该协议
Then have your presenting view controller adopt the protocol
in your .h
MyPresentingViewController : UIViewController <MyProtocol>
in .m
//Implement method:
-(void)updateValues:(NSArray *)values {
//Update Values.
}
最后,当用户按下完成"时
Finally when user presses "Done"
您可以打电话
[delegate updatedValues:myValues];
并进行相应的更新.
希望有帮助.
这篇关于从presentModalViewController取回时如何更新滑块的值并进行切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!