将 Interface Builder 创建的视图添加到以编程方式创建的视图 - 可以吗?

发布于 2024-09-11 11:49:52 字数 422 浏览 2 评论 0原文

好吧,我已经下载了一个开源项目,我正在考虑定制供个人使用(除非它变成一个伟大的项目,在这种情况下谁知道),但无论哪种情况我都遇到了一些困难。

从一开始我就发现使用编程方法创建 UI 元素相当困难,只是找不到一个好的教程(如果有人有任何建议,我们将不胜感激!)

所以我得出的结论是,也许我最好的方法是采用我想要修改的视图,在 Interface Builder 中重新创建它,然后将其添加到以前的视图中,这里的问题是我似乎不知道如何做到这一点。如果有人想查看代码,我可以提供它,但它基于 RemotePad 开源项目(很容易通过谷歌搜索),并且我正在考虑替换 TapView 元素 - 实际上我需要做的就是在三个鼠标下添加第四个按钮按钮,但它让我迷失了。

我想我真正要问的是添加第四个按钮的最佳方法是什么?理想情况下,按钮应该是“可换肤”的,即应该采用可以应用突出显示模式的图像形式。

Ok so I've downloaded an open source project that I'm looking at customising for personal use (unless it turns into a great project in which case who knows) but in either case I'm having some difficulty.

From a starting point I'm finding creating UI elements using progmatic methods rather difficult, just can't find a good tutorial out there (if anyone has any suggestions it would be much appreciated!)

So I've come to the conclusion that maybe my best method is to take the view that I wish to modify, recreate it in Interface Builder, then add it to the previous view, the problem here is that I just can't seem to get my head around how to do this. If anyone wants to see the code I can provide it but it's based on the RemotePad open source project (easily googled) and I'm looking at replacing the TapView elements - really all I need to do is add a fourth button under the three mouse buttons but it's just lost me.

I suppose really what I'm asking is what's the best way for me to go about adding this fourth button? Ideally the button should be 'skinable' i.e. should be in the form of an image that can have a highlighted mode applied.

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

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

发布评论

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

评论(1

诠释孤独 2024-09-18 11:49:52

这实际上应该很容易做到。

  1. 首先,您需要为新按钮设置现有视图控制器(与要添加按钮的视图关联的视图控制器)和 IBOutlet。所以你添加类似的内容:
    @property(非原子,保留)IBOutlet UIButton *myFourthButton;
    
  2. 接下来,使用 Interface Builder 创建 nib 文件。从一个空的 IB 文件开始,并向其中添加您的按钮。您还需要将文件的所有者设置为视图控制器类的实例。然后将文件所有者的 myFourthButton 插座连接到您的新按钮。保存 IB 文件。

  3. 现在您需要某种方法在创建视图控制器时加载此 NIB 文件。我建议通过调用以下方法在视图控制器的 viewDidLoad: 方法中执行此操作:

    [[NSBundle mainBundle] loadNibNamed:@"yourNibFile" 所有者:self 选项:nil];
    
  4. NIB 文件中的按钮现在应该连接到您的 myFourthButton 插座,现在您只需将其添加到视图并定位它即可。下面我将其添加到视图控制器主视图中。但是,您可能应该将其添加到一个子视图(取决于原始视图的设置方式)。同样,我会将此代码放在 viewDidLoad: 之后,以编程方式设置视图的所有现有代码之后(或者如果该代码位于其他地方,则放在另一种方法中)。

    [self.view addSubview:myFourthButton];  
    
    CGRect 框架 = myFourthButton.frame;
    框架.origin.x = 100;
    框架.origin.y = 100;
    myFourthButton.frame = 框架;
    
    
    

    当您需要按钮实际响应点击事件时,您可以使用 IBAction 和 Interface Builder 将其连接到视图控制器,正如您所期望的那样。

This should actually be fairly easy to do.

  1. First, you need to set up the existing view controller (the one associated with the view where you want to add your button) with an IBOutlet for your new button. So you add something like:
    @property (nonatomic, retain) IBOutlet UIButton *myFourthButton;
    
  2. Next, create the nib file with Interface Builder. Start with an empty IB file and add your button to it. You also need to set the File's Owner to be an instance of the view controller class. Then connect the File Owner's myFourthButton outlet to your new button. Save the IB file.

  3. Now you need some way to load this NIB file when the view controller is created. I would suggest doing this in the view controller's viewDidLoad: method by calling:

    [[NSBundle mainBundle] loadNibNamed:@"yourNibFile" owner:self options:nil];
    
  4. The button from the NIB file should now be connected to your myFourthButton outlet, now you just need to add it to the view and position it. Below I add it to the view controllers main view. However, there may be a subview that you should add it to instead (depends on how the original view is set up). Again, I would put this code in viewDidLoad: after all of the existing code to set up the view programmatically (or in another method if that code is elsewhere).

    [self.view addSubview:myFourthButton];  
    
    CGRect frame = myFourthButton.frame;
    frame.origin.x = 100;
    frame.origin.y = 100;
    myFourthButton.frame = frame;
    

    When you need your button to actually respond to a tap event, you can connect it to your view controller using an IBAction and Interface Builder just as you would expect.

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