In my ViewController I have created a view which contains a button and a bunch of other elements. When the button is pressed I want to call a method from the parent ViewController. I tried:
在我的ViewController中,我创建了一个视图,其中包含一个按钮和一堆其他元素。按下按钮时,我想从父ViewController调用一个方法。我试过了:
[self.superview buttonPressedMethod];
But the superview is no the ViewController but UIView. Is there anyway to do this?
但是superview不是ViewController而是UIView。反正有没有这样做?
3 个解决方案
#1
1
This is exactly what the target/action mechanism is for. Make the method in the view controller an IBAction. Set the view controller as the button's target, and set the method as its action. That should be all you need to do.
这正是目标/行动机制的用途。使视图控制器中的方法成为IBAction。将视图控制器设置为按钮的目标,并将方法设置为其操作。这应该就是你需要做的。
#2
0
At ParentViewController.h
-(void)ParentMethod;
At ParentViewController.m
-(IBAction)button:(id)sender {
[self ParentMethod];
}
At IBAction of Play Button on child view controller do this:
-(IBAction)ChildMethod:(id)sender {
ParentViewController *parent=self.parentViewController;
[parent ParentMethod];
}
#3
0
Better create delegate in view controller and set delegate in child view, so when something occur call delegate method according to it.
更好地在视图控制器中创建委托并在子视图中设置委托,因此当出现问题时,根据它调用委托方法。
#1
1
This is exactly what the target/action mechanism is for. Make the method in the view controller an IBAction. Set the view controller as the button's target, and set the method as its action. That should be all you need to do.
这正是目标/行动机制的用途。使视图控制器中的方法成为IBAction。将视图控制器设置为按钮的目标,并将方法设置为其操作。这应该就是你需要做的。
#2
0
At ParentViewController.h
-(void)ParentMethod;
At ParentViewController.m
-(IBAction)button:(id)sender {
[self ParentMethod];
}
At IBAction of Play Button on child view controller do this:
-(IBAction)ChildMethod:(id)sender {
ParentViewController *parent=self.parentViewController;
[parent ParentMethod];
}
#3
0
Better create delegate in view controller and set delegate in child view, so when something occur call delegate method according to it.
更好地在视图控制器中创建委托并在子视图中设置委托,因此当出现问题时,根据它调用委托方法。