我的viewController中有一个containerView。我的viewController有一个UISegmentedControl。
如何在此时以及每次更改时选择此selectedSegmentIndex?
我',尝试使用prepareForSegue在下一个类中的属性,但这只在第一次加载时发送.. 谢谢!
然后编辑我得到: - [ViewController containerViewDidChangeSegmentIndex:]:无法识别的选择器发送到实例0x7f8db16286d0
FirstViewController(包含一个select和一个containerView)
ViewController.h
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *select;
@property (weak, nonatomic) IBOutlet UIView *container;
@property(nonatomic, assign) ContainerViewController * classLevelReference;
@end
ViewController.m
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_select addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentChanged:(UISegmentedControl *)segment{
//since you've reference to your container view here, you can directly call its method here:
[self.classLevelReference containerViewDidChangeSegmentIndex:segment.selectedSegmentIndex];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//don't forget to check the segue id, if you've multiple segues
ContainerViewController *containerView = [segue destinationViewController];
self.classLevelReference = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ContainerViewController.h
@interface ContainerViewController : UIViewController
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex;
@end
ContainerView.m
@implementation ContainerViewController
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex{
//Do whatever you want with your updated index
NSLog(@"changing");
}
@end
答案 0 :(得分:1)
您可能希望在ContainerViewController.h
中创建一个方法,如下所示:
@interface ContainerViewController:UIViewController
//.....other implementation here.
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex;
@end
现在在ContainerViewController.m
中实现此方法,如下所示:
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex{
//Do whatever you want with your updated index
}
现在,在准备segue时,请在类级变量中保存对ContainerViewController
的引用:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//don't forget to check the segue id, if you've multiple segues
ContainerViewController *containerView = [segue destinationViewController];
self.classLevelReference = containerView;
}
最后在FirstViewController.m
中,告诉容器视图何时段索引发生变化。
- (void)viewDidLoad{
//.....your other implementation here.....
//add a listener to your segment's value changed action
[youregements addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentChanged:(UISegmentedControl *)segment{
//since you've reference to your container view here, you can directly call its method here:
[self.classLevelReference containerViewDidChangeSegmentIndex:segment.selectedSegmentIndex];
}
重要提示:在您的案例中,ContainerViewController
和FirstViewController
可能有不同的名称,只需仔细应用更改。
快乐的编码!