Storyboard,将指向 IBAction 的链接从容器视图拖到父视图 [英] Storyboard, drag link to an IBAction from a container view to the parent

查看:24
本文介绍了Storyboard,将指向 IBAction 的链接从容器视图拖到父视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个名为 Club 的微不足道的 VC

Here's a trivial VC called Club

俱乐部有一个功能:

@implementation Club
-(IBAction)clickMe
    {
    NSLog(@"Whoa!");
    }
@end

现在关于 ButtonA.显然,在 Storyboard 中,您可以将从 ButtonA 拖到Club"中的clickMe"函数中.

Now regarding ButtonA. Obviously in Storyboard you can drag from ButtonA to the function "clickMe" in "Club".

现在.关于ButtonB.

Now. Regarding ButtonB.

在 Storyboard 中,有没有什么方法可以从 ButtonB 拖动,到Club"中的clickMe"?

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

然后,您可以从 ButtonB 拖动到 sameAsClickMe.但这完全是一件麻烦事.

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.

在 Storyboard 中是否有一种晦涩的方法可以做到这一点?干杯!

Is there an obscure way to do this in Storyboard? Cheers!

只是 FTR,仅限 iOS7+,没什么旧的

Just FTR, iOS7+ only, nothing older

注意 - 朝着另一个方向"前进是经过充分探索的,并且很容易做到.

Note - going in the "other direction" is well-explored and easy enough to do.

推荐答案

不,你不能这样做,但你可以使用委托来处理这种行为.

No, you can't do that but you can use delegates for this behavior.

在 SecondViewController.h 中:

@protocol SecondViewControllerDelegate <NSObject>
-(void)clickMe;
@end

@interface SecondViewController.h: UIViewController

@property (weak, nonatomic) id <SecondViewControllerDelegate> delegate;

@end

在 SecondViewController.m 中:

@implementation SecondViewController 

- (IBAction) buttonBClicked:(id)sender {
 if ([self.delegate respondsToSelector:@selector(clickMe)] {
       [self.delegate performSelector:@selector(clickMe)];
    }
}

@end

在 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

尝试使用 Unwind Segues!我已经在此处发布了有关如何使用它们的答案.但跳过第 4 步.

Try with Unwind Segues! I've posted an answer here on how to use them. But skip step 4.

这篇关于Storyboard,将指向 IBAction 的链接从容器视图拖到父视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆