简体   繁体   English

如何从一个XIB中的另一个UIViewController文件控制第二个UIView(子视图)

[英]How to controll second UIView (subview) from a different UIViewController file in one XIB

I am strugling on the following task. 我正在努力完成以下任务。

I am trying to control my subview from another viewController class. 我试图从另一个viewController类控制我的子视图。 What I did and does not work is this. 我所做的和不行的是这个。

I inserted an object and changed it class to my second viewController class. 我插入了一个对象,并将其类更改为第二个viewController类。 Then I connected its UIButton outlet to a button I have on my subview. 然后,将其UIButton插座连接到子视图上的按钮。 I then connected the buttons action to the outlet of my second view controller. 然后,将按钮动作连接到第二个视图控制器的插座。

What I get when I run is this. 我跑步时得到的是这个。 It all shows up well but when I try to touch the button that resides in my subview app crashes. 一切都很好显示,但是当我尝试触摸位于子视图应用程序中的按钮时崩溃。 I am only left with a worringing: "Action unavailable: The "Touch Up Inside" event of "Rounded Rect Button". 我只剩下一个令人担忧的地方:“操作不可用:“ Rected Rect Button”的“ Touch Up Inside”事件。

It's probably my logic that is incorrect. 我的逻辑可能不正确。 Thanks for help. 感谢帮助。

Well after a long research I got an answer to my problem. 经过长时间的研究,我得到了我问题的答案。 As it appears I was doing everything right. 看起来我在做所有正确的事情。

The problem is that after the initial XIB file is initialized it autoreleases all views and subviews. 问题在于初始化初始XIB文件后,它会自动释放所有视图和子视图。 So to prevent from gettig your second view controller from being released implement this method 因此,为了防止gettig您的第二个视图控制器被释放,请实现此方法

 - (void)awakeFromNib {
    [self retain];
}

in your view controller .m file. 在您的视图控制器.m文件中。 This method will retain your second view controller alive and allow it to recive and respond to UI actions. 此方法将使您的第二个视图控制器保持活动状态,并允许其重新接收并响应UI动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM