簡體   English   中英

從子視圖返回一個Int到ViewController

[英]Returning an Int from a subview to the ViewController

我有一個UIView添加到我的rootViewController中,該UIView檢測到觸摸並且使用它們的坐標為我返回了一個特定的Integer,但是我不知道如何將該整數返回給我的rootViewController。 我在某處讀到MVC表示UIView不應該了解ViewController,這是真的嗎? 如果那么我該怎么做呢? 謝謝!

我建議您定義一個在它們之間傳遞Integer的協議。 使您的視圖控制器實現該協議並將其分配給視圖。 然后,從視圖中用所需的值觸發協議方法。

協議示例:

@protocol YourProtocol

-(void)handleCaptureInteger:(NSInteger)value;

@end

視圖控制器(.h)示例:

@interface YourViewControler : UIViewController<YourProtocol>

...

@end

視圖控制器(.m)示例:

@implementation YourViewController

- (void)viewDidLoad
{
    [yourView setDelegate:self];
}

@end

查看示例:

@interface YourView : UIView

@property (nonatomic, assign) NSObect<YoutProtocol> *delegate;

@end

我建議將所有這些邏輯放在ViewController上。 讓rootViewController將TapGestureRecognizer添加到視圖,並使用其locationInView:和/或locationOfTouch:inView:方法來確定應用程序應如何響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM