问题
Here's a trivial VC called Club

Club has a function:
@implementation Club
-(IBAction)clickMe
{
NSLog(@"Whoa!");
}
@end
Now regarding ButtonA. Obviously in Storyboard you can drag from ButtonA to the function "clickMe" in "Club".
Now. Regarding ButtonB.
Is there any way, in Storyboard, to drag from ButtonB, to "clickMe" in "Club"?
Perhaps using the mysterious "object" objects, or ... ??
Note that, obviously, you can make a class for the small view, and have a function:
@implementation SmallViewOnRight
-(IBAction)sameAsClickMe
{
[(Club*)self.parentViewController clickMe];
}
@end
Then, you can drag from ButtonB to sameAsClickMe. But that's a complete nuisance.
Note that it's very normal to use container views like this, to handle different "areas" of your main view (particularly if you have stuff sliding around and so on, and when you have many things "on top of each other"). It's hugely convenient to move "sections" outside the main view, using container views. But it's a complete nuisance "passing up" the clicks.
Is there an obscure way to do this in Storyboard? Cheers!
Just FTR, iOS7+ only, nothing older
Note - going in the "other direction" is well-explored and easy enough to do.
回答1:
No, you can't do that but you can use delegates for this behavior.
In SecondViewController.h:
@protocol SecondViewControllerDelegate <NSObject>
-(void)clickMe;
@end
@interface SecondViewController.h: UIViewController
@property (weak, nonatomic) id <SecondViewControllerDelegate> delegate;
@end
In SecondViewController.m:
@implementation SecondViewController
- (IBAction) buttonBClicked:(id)sender {
if ([self.delegate respondsToSelector:@selector(clickMe)] {
[self.delegate performSelector:@selector(clickMe)];
}
}
@end
In ClubViewController.m:
#import "SecondViewController.h"
@interface ClubViewController () <SecondViewControllerDelegate>
@end
@implementation ClubViewController.m
// make yourself a delegate of SecondViewController somewhere in your code. For example, in prepareForSegue.
-(void)clickMe {
NSLog (@"Clicked");
}
@end
EDIT: Try with Unwind Segues! I've posted an answer here on how to use them. But skip step 4.
来源:https://stackoverflow.com/questions/26043973/storyboard-drag-link-to-an-ibaction-from-a-container-view-to-the-parent