保持對其他視圖控制器的引用是否正常?

[英]Is it normal to keep a reference to other viewcontrollers?


Is it worth storing references to other viewcontrollers that the current viewcontroller opens? If I have one viewcontroller displaying a screen with a button that opens a popoverviewcontroller, should I then keep a reference to it and then check afterwards to see if the variable is nil to only make the popoverviewcontroller once? Or is it unnecessary? It lead to many extra variables in some cases, and additional code, but it would be great if someone could say if this is unnecessary or not.

是否值得存儲當前viewcontroller打開的其他viewcontrollers的引用?如果我有一個viewcontroller顯示一個帶有打開popoverviewcontroller的按鈕的屏幕,那么我應該保留對它的引用,然后檢查該變量是否為nil只能使popoverviewcontroller一次?還是沒必要?在某些情況下會導致許多額外的變量,以及額外的代碼,但如果有人可以說這是否是不必要的話會很好。

if (self.popoverVC == nil) {
    UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:self.configureTest];
    popVC.popoverContentSize = CGSizeMake(320.0, 460.0);
    popVC.delegate = self;

    self.popoverVC = popVC;
    [popVC release];
}

3 个解决方案

#1


3  

Storing references to other controllers is fine with me and I do it whenever it seems reasonable to me. Think of the delegate pattern as a way to model the interaction between your controllers.

存儲對其他控制器的引用對我很好,只要對我來說合理,我就會這樣做。將委托模式視為模擬控制器之間交互的一種方式。

There are alternatives, though.

不過還有其他選擇。

In you specific case, instead of storing the reference to the controller, you could set a flag (but I don't know if this makes sense for your specific workflow) specifying that the button was pressed.

在您特定的情況下,您可以設置一個標志(但我不知道這對您的特定工作流程是否有意義),而不是將引用存儲到控制器中,指定按下按鈕。

Not applicable in your case but possibly useful in general is accessing the "parent" hierarchy of UIViewControllers. This is not explicitly maintained by UIKit, so either you set the parentViewController property or you can go from one controller to its parent navigating the UIResponder hierarchy. This is not applicable to your case, it seems to me, because communication would be initiated by the child controller (in your case you are interested in the opposite). Anyway, google for it, if you are interested.

在您的情況下不適用但通常可能有用的是訪問UIViewControllers的“父”層次結構。這並沒有由UIKit明確維護,因此要么設置parentViewController屬性,要么可以從一個控制器轉到其父級導航UIResponder層次結構。這似乎不適用於您的情況,在我看來,因為通信將由子控制器啟動(在您的情況下,您對相反的興趣)。無論如何,google for it,如果你有興趣的話。

Another option you have is using notifications: one controller can register for notifications, another can observe it.

您擁有的另一個選項是使用通知:一個控制器可以注冊通知,另一個可以觀察通知。

  [[NSNotificationCenter defaultCenter] postNotificationName:@"notifName" object:self];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reactToNotif:) name:@"notifName" object:nil];

This is a bit more expensive than a function call, but it requires no ivar and no coupling between controllers.

這比函數調用貴一點,但它不需要ivar,也不需要控制器之間的耦合。

#2


2  

That's perfectly sensible, especially if you want popVC to maintain its state between appearances. It would be wise to release inactive vc's when you get a memory warning.

這是非常明智的,特別是如果你想讓popVC在外觀之間保持其狀態。當你收到內存警告時,發布非活動的vc是明智的。

#3


1  

The reason for keeping a reference to UIPopoverController (btw it's not a subclass of UIViewController, it's a subclass of NSObject) is a slightly different one than for keeping a reference to a UIViewController.

保持對UIPopoverController的引用的原因(btw它不是UIViewController的子類,它是NSObject的子類)與保持對UIViewController的引用略有不同。

When you present a UIPopoverController it is not automatically retained. So you have to retain it yourself. Compared to UIViewController that are automatically retained when you present (or push) them.

當您呈現UIPopoverController時,它不會自動保留。所以你必須自己保留它。與UIViewController相比,當您呈現(或推送)它們時會自動保留。

There were tricks that just retained the controller without keeping a reference and released it in the delegate method that informed you that the popover was dismissed.

有些技巧只是保留了控制器而沒有保留引用,並在委托方法中釋放它,告知您彈出窗口已被解除。

For me, those tricks felt always a little bit dirty. For example you didn't have the option to manually dismiss the popover.

對我來說,這些技巧總覺得有點臟。例如,您沒有選擇手動關閉彈出窗口。


Since ARC is publicly documented on the LLVM page I can tell you that those tricks stop to work if you use ARC. You have to keep a reference otherwise the compiler will release it immediately after you have left the method that presents the popover.

由於ARC在LLVM頁面上公開記錄,我可以告訴您,如果您使用ARC,那些技巧就會停止工作。您必須保留引用,否則編譯器將在您離開顯示彈出窗口的方法后立即釋放它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/06/25/724d149edfac6c9ef766e3f69e5c00a8.html



mvc在哪里保持對控制器的引用? - mvc where to keep the reference to controllers? 視圖控制器層次結構中IBOutlet子視圖控制器的引用類型(弱或強)? - Reference type (weak or strong) for for IBOutlet child viewcontrollers in a viewcontroller hierarchy? Swift - 如何在實例化新視圖控制器時保持對舊視圖控制器的引用? - Swift - How to keep reference to old view controller when instantiating new view controller? 我何時必須保留對我的控制器或視圖的引用,何時不是?見例子 - When do I have to keep a reference to my controller or view and when not? See examples 保持導航控制器的狀態,只改變視圖 - Keep state of navigation controller and only change view 保持任務在視圖控制器之間運行 - Keep task running between view controllers 為所有視圖控制器定制導航控制器。 - Customizing Navigation Controller for all ViewControllers 獲取對容器視圖的視圖控制器的引用[重復] - Getting reference to view controller of the container view [duplicate] 在UINavigationController中設置幀視圖控制器視圖 - set frame viewcontrollers view in UINavigationController 在多個視圖控制器上保持鍵盤可見,並專注於每個textfield - Keep keyboard visible on multiple view controllers and focused on every textfield
 
粤ICP备14056181号  © 2014-2020 ITdaan.com