如何以编程方式将 UIButton 添加到 UIToolBar?

发布于 2024-11-29 21:15:23 字数 1142 浏览 0 评论 0原文

我已经使用 Interface Builder 添加了工具栏,但我需要在运行时/有条件地添加按钮。我没有收到任何错误,但我的动态按钮没有出现在工具栏上。我已经验证 arrayOfModulesScreens 已加载我需要的数据。至少那是有效的(:))。我是否需要将按钮添加到 UIView 中,然后将该视图添加到工具栏?只是想出来而已。也许有更好的方法开始?预先感谢您提供任何有助于解决问题的线索。

CustomFormController.h

@interface CustomFormController : UIViewController { 
    UIToolbar *screensBar;  
}

CustomFormController.m

EPData *epData = [[EPData alloc] init];
NSArray *screens = [epData loadPlistIntoArray:@"Screens"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"process_module_id == %@", process_modulesID];
NSArray *arrayOfModulesScreens = [screens filteredArrayUsingPredicate:predicate];

for(int i=0; i < [arrayOfModulesScreens count]; i++) {
    NSDictionary *dictRow = [arrayOfModulesScreens objectAtIndex:i];
    UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [button setTitle:[dictRow objectForKey:@"screen_title"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [screensBar addSubview:button];  
}

I have added the toolbar using Interface Builder, but I need to add the buttons at runtime / conditionally. I'm not getting any errors, but my dynamic buttons are not appearing on the toolbar. I have verified that arrayOfModulesScreens is loaded with the data I need. At least that works (:)). Do I need to add the buttons into a UIView, then add that view to the toolbar? Just thinking outloud. Perhaps there is a better approach to begin with? Thanks in advance for any clues leading to the resolve.

CustomFormController.h

@interface CustomFormController : UIViewController { 
    UIToolbar *screensBar;  
}

CustomFormController.m

EPData *epData = [[EPData alloc] init];
NSArray *screens = [epData loadPlistIntoArray:@"Screens"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"process_module_id == %@", process_modulesID];
NSArray *arrayOfModulesScreens = [screens filteredArrayUsingPredicate:predicate];

for(int i=0; i < [arrayOfModulesScreens count]; i++) {
    NSDictionary *dictRow = [arrayOfModulesScreens objectAtIndex:i];
    UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [button setTitle:[dictRow objectForKey:@"screen_title"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [screensBar addSubview:button];  
}

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

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

发布评论

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

评论(3

棒棒糖 2024-12-06 21:15:23

如果您实际上想向工具栏添加 UIBarButtonItem(不是 UIButton),只需创建一个或多个 UIBarButtonItem,将它们放入 NSArray(或 NSMutableArray)中,然后将该数组分配给 items工具栏的属性。有关详细信息,请参阅UIBarButtonItem 文档 。使用上面的代码,可能看起来像这样:(

    NSMutableArray *items = [NSMutableArray array];
    for (int i = 0; i < [arrayOfModulesScreens count]; i++) {
        NSDictionary *dictRow = [arrayOfModulesScreens objectAtIndex:i];
        UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:[dictRow objectForKey:@"screen_title"]
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(buttonClick:)];
        [items addObject:button];
        [button release];
    }
    screensBar.items = items;

当然,然后您需要更改 buttonClick: 以期望 UIBarButtonItem 而不是 UIButton)。

如果您确实想在其中放置一个 UIButton,则首先需要将 UIButton 包装在 UIBarButtonItem 中,如下所示:

UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

然后将项目添加到工具栏,如上所示。


至于为什么您的按钮没有显示在您发布的代码中,问题是 UIButton 的 buttonWithType: 创建了一个零宽度和零高度的按钮。您需要调整按钮的大小(手动或在设置标题后使用 sizeToFit)以使其显示。修复此问题后,您会看到按钮全部位于父视图左上角的彼此之上;您需要根据需要手动放置它们。

If you are actually wanting to add a UIBarButtonItem (not a UIButton) to the toolbar, you just create one or more UIBarButtonItems, put them in an NSArray (or NSMutableArray), and assign that array to the items property of the toolbar. See the UIBarButtonItem documentation for details. Using your code above, that might look something like this:

    NSMutableArray *items = [NSMutableArray array];
    for (int i = 0; i < [arrayOfModulesScreens count]; i++) {
        NSDictionary *dictRow = [arrayOfModulesScreens objectAtIndex:i];
        UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:[dictRow objectForKey:@"screen_title"]
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(buttonClick:)];
        [items addObject:button];
        [button release];
    }
    screensBar.items = items;

(you would, of course, then need to change your buttonClick: to expect a UIBarButtonItem instead of a UIButton).

If you're really wanting to put a UIButton in there, you first need to wrap the UIButton in a UIBarButtonItem, something like this:

UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

Then add the item to the toolbar as above.


As for why your buttons aren't showing up in your posted code, the problem is that UIButton's buttonWithType: creates a button with zero width and zero height. You would need to resize the button (manually or by using sizeToFit after setting the title) to make it show. After fixing that, you would then see that the buttons are all on top of each other in the upper-left corner of the parent view; you would need to position them manually as appropriate.

银河中√捞星星 2024-12-06 21:15:23

我认为您不能像这样直接在代码中更改 navigationController

更安全的方法是这样的:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:[dictRow objectForKey:@"screen_title"]
                                                           style:UIBarButtonItemStyleBordered
                                                          target:self
                                                          action:@selector(buttonClick:)];
self.navigationItem.rightBarButtonItem = button;

I don't think you can change the navigationController directly in your code like that.

A safer way would be this:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:[dictRow objectForKey:@"screen_title"]
                                                           style:UIBarButtonItemStyleBordered
                                                          target:self
                                                          action:@selector(buttonClick:)];
self.navigationItem.rightBarButtonItem = button;
千笙结 2024-12-06 21:15:23

ToolBar 方法

UIButton 添加到 UIToolBar 的一个问题是按钮的宽度和按钮的位置很难控制

更简单的方法

删除工具栏并放置一个 UIView ,其背景图像与工具栏相同。现在您可以将 UIButton 添加为该视图的子视图。这样您就可以更好地控制按钮的位置和宽度。您也可以添加其他视图。

ToolBar Approach

One problem with adding UIButton to UIToolBar is that width of the button and positioning of the buttons is difficult to control

Simpler Approach

Remove the toolbar and place a UIView with the background image which is identical to your toolbar. Now you can add the UIButton as a subview to that view. This way you have more control over the positioning and the width of the buttons. Also you can add other views as well.

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