其他 UIviewController 和 IBActions 中的 UIViewController

发布于 2024-10-22 04:16:59 字数 944 浏览 8 评论 0原文

我正在设计一个 iPad 应用程序,其中的小 UIScrollViews 有自己的 UIViewController 。这些控制器在其视图中有一个调用 IBAction 方法的按钮。但它不起作用,事实上,它们似乎并没有在模拟器中被按下。

这里有一些代码可以让您了解我正在做什么。

// In UIViewController A (say the parent or root that have several UIScrollViews)

    MiniViewController * mini = [[MiniViewController alloc]init];
    [scrollView1 addSubview:mini.view];

//repeat the same process a couple of times with different uiscrollsviews and instances of miniviewcontrollers

现在,正如您所猜想的那样,MiniController 非常简单,我只发布了 .h 文件。

@interface MiniControlador : UIViewController {
     IBOutlet UIButton * button;
}
@property (nonatomic, retain) IBOutlet UIButton * button;
- (IBAction)doSomething:(id)sender;
@end

您可以看到我使用 Interface builder 将 UIButton“按钮”连接到名为 doSomething 的方法。但正如我已经说过的,它不起作用。

还有一件事。我还尝试以编程方式使用 Mini Controller 实例向 UIScrollView 添加一个按钮。它成功了!但我当然相信它是极其硬编码的。

你怎么认为?我将不胜感激任何建议。

I'm designing an iPad app where little UIScrollViews have their own UIViewController . Those controllers have in their view a button that call an IBAction method. But it isnt working, in fact, it doesnt seem that they are being pressed in the simulator.

Here is some code to give you an idea of what im am doing .

// In UIViewController A (say the parent or root that have several UIScrollViews)

    MiniViewController * mini = [[MiniViewController alloc]init];
    [scrollView1 addSubview:mini.view];

//repeat the same process a couple of times with different uiscrollsviews and instances of miniviewcontrollers

Now The MiniController is very simple as you can guess, i only post the .h file

@interface MiniControlador : UIViewController {
     IBOutlet UIButton * button;
}
@property (nonatomic, retain) IBOutlet UIButton * button;
- (IBAction)doSomething:(id)sender;
@end

You can see that i used Interface builder to connect an UIButton "button" to a method called doSomething. But as i already said, it isnt working.

One more thing. I also tried to add a button to the UIScrollView with the Mini Controller instance programmatically.And it worked! But I certainly believe that it's extremely hardcoded.

What do you think? I'll appreciate any suggestion(s).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2024-10-29 04:16:59

Apple 的 View Controller 编程指南 是一本重要的读物,并详细解释了苹果公司“每屏一个视图控制器”的理念。

视图控制器的大部分行为都是建立在一次只有一个视图控制器在运行的假设之上的。当违反该假设时,行为是未定义的(或至少是未记录的)。在这种情况下,您的描述表明将控制器插入其根视图和根的超级视图(通常是前一个屏幕)之间的响应者链的正常视图控制器行为不起作用。

虽然您可能会发现初始化方法可以正常工作,但不能保证它们一定能工作,并且行为可能会随着未来操作系统的更新而改变。

编辑:视图控制器编程指南中的相关引用:

您创建的每个自定义视图控制器对象负责管理单个视图层次结构中的所有视图。在 iPhone 应用程序中,视图层次结构中的视图传统上覆盖整个屏幕,但在 iPad 应用程序中它们可能仅覆盖屏幕的一部分。视图控制器与其视图层次结构中的视图之间的一一对应是关键的设计考虑因素。您不应使用多个自定义视图控制器来管理同一视图层次结构的不同部分。同样,您不应该使用单个自定义视图控制器对象来管理多个屏幕的内容。

Apple's View Controller Programming Guide is an important read, and explains a lot about Apple's philosophy of one-view-controller-per-screen.

Much of the behavior of view controllers is built on the assumption that there is only one view controller operating at a time. When that assumption is violated, the behavior is undefined (or at least undocumented). In this case, your description suggests that the normal view controller behavior of inserting the controller into the responder chain between its root view and that root's superview (usually the previous screen) isn't working.

While you may find methods of initialization that do work properly, they're not going to be guaranteed to work, and the behavior is liable to change with future OS updates.

Edit: A relevant quotes from the View Controller Programming Guide:

Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.

始终不够爱げ你 2024-10-29 04:16:59

谢谢大家,我终于使用类的对象(我称之为 GenericViewController)解决了这个问题。它实际上就像一个常规的 UIViewController,IBActions 可以很好地响应任何事件(即按下按钮)。

我使用 IBOutlet UIView 来包含 UILabels、按钮...等等。

如果有人感兴趣的话,这里有一些代码。

@interface GenericViewController : NSObject {
  /* Some IBOutlets here*/

     //like a regular UIView of an UIViewController, this holds the rest of the outlets
  IBOutlet UIView * view;
  } 
   //some IBActions here
  }

然后 UIScrollView 只添加每个 GenericViewController 对象的视图

[scrollView addSubview:genericViewControllerObject.view];

如果有人有更好的解决方案,请告诉我:)

Thanks guys, I finally solve this using objects of a class (that I called GenericViewController). It actually acts like a regular UIViewController, the IBActions responds well to any event (i.e. buttons being pressed).

I used an IBOutlet UIView in order to contain UILabels, buttons...and so on.

Here is some code if anyone is interested.

@interface GenericViewController : NSObject {
  /* Some IBOutlets here*/

     //like a regular UIView of an UIViewController, this holds the rest of the outlets
  IBOutlet UIView * view;
  } 
   //some IBActions here
  }

Then the UIScrollView only add the view of each GenericViewController object

[scrollView addSubview:genericViewControllerObject.view];

If anyone has a better solution, please let me know :)

只涨不跌 2024-10-29 04:16:59

您确定正在从 InterfaceBuilder 中创建的 xib 加载视图吗?
我正在我的应用程序中做类似的事情,它对我有用。
我正在实现这样的 init 方法:


- (id)init
{
    if (self = [super initWithNibName:@"__your_xib_name__" bundle:[NSBundle mainBundle]])
    {
        // TODO: Add additional initializing here
        // ...
    }

    return self;
}

如果您没有从 xib 加载视图,则不会建立任何连接(不会初始化 IBOutlets,也不会触发 IBActions)。

Are you sure you are loading the view from the xib you made in InterfaceBuilder?
I'm doing something similar in my app, and it's working for me.
I'm implementing the init method like this:


- (id)init
{
    if (self = [super initWithNibName:@"__your_xib_name__" bundle:[NSBundle mainBundle]])
    {
        // TODO: Add additional initializing here
        // ...
    }

    return self;
}

If you are not loading the view from the xib, then there will be no connections made (no IBOutlets initialized and no IBActions triggered).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文