如何定义和发送方法消息,该消息将动态地将UibarbuttonItems添加到Uitoolbar



我已经对UitoolBar的UibarbuttonItem进行了动态添加,现在我尝试将其作为方法-(void)AddBarButton:withTitle (NSString*)title实现并从事件处理程序中调用(单击Uisearchbar搜索结果行))。问题是我不知道如何实现添加uibarbutton作为一种单独的方法,而最重要的如何称呼它(发送)?谁将成为此消息的接收者? [self.toolBar addButton]不起作用。

我想为专业iOS开发人员的问题而道歉,因为我是C java程序员已有7年了,现在我必须在短时间内没有任何经验而没有任何经验。所以我真的需要帮助。

预先感谢!

我建议在您的方法中添加更多参数,以使其更灵活(如果您不需要或需要,则可以将其从声明中删除并用它们替换为方法实现中的固定值)。示例实现可能是这样的:

- (void)addBarButtonWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action animated:(BOOL)animated
{
    UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:title style:style target:target action:action];
    NSMutableArray *items = [self.toolBar.items mutableCopy];
    [items addObject:newButton];
    [self.toolBar setItems:items animated:animated];
}

在您的活动处理程序中,您然后致电(根据需要修改参数):

[self addBarButtonWithTitle:@"someTitle" style:UIBarButtonItemStyleBordered target:self action:@selector(someMethod:) animated:YES];

最新更新