如何使用具有多个参数的方法来创建自定义按钮
我重复我的代码几次来创建自定义按钮,我试图创建一个方法并通过调用该方法并使用两个参数 btnTitle 和 btnAction 来创建所有所需的按钮。
我的方法代码
-(void) addnewButton:(NSString *) btnTitle withAction:UIAction btnAction; {
// Add a custom edit navigation button
editButton = [[[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString((btnTitle), @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(btnAction)] autorelease];
self.navigationItem.rightBarButtonItem = editButton;
}
现在我该如何调用这个方法来创建一个按钮?
I am repeating my codes few times to create custom buttons, I am trying to create a method and create all my required buttons just by called that method and using two parameters, btnTitle and btnAction.
My Codes for the method
-(void) addnewButton:(NSString *) btnTitle withAction:UIAction btnAction; {
// Add a custom edit navigation button
editButton = [[[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString((btnTitle), @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(btnAction)] autorelease];
self.navigationItem.rightBarButtonItem = editButton;
}
Now how shall I call this method to create a button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不太确定这里的问题是什么?
当应用程序的设计完成后,我构建了一个 ISInterfaceElements 类,其中包含我在应用程序中需要的东西。 (UIlabels、UIColors、UIButtons 等)UIElements 的实例化只会占用空间,使问题变得复杂,如果您需要更改在 14 个位置使用的按钮或颜色,那么在不同的位置编写所有设置代码确实很糟糕地方。无论如何,我通常这样做:
使用漂亮的枚举来使其易于记住。
因为这些都是类方法,所以我不需要实例化 ISInterfaceElement 类来使用它们。
我只是说:
你可以为你的按钮构建类似的东西,你需要做的就是在你的方法中构建一个新的 UIButton,设置标题和操作,最后返回 myButton,就像我对标签所做的那样。
希望有帮助
Not quite sure what the question is here?
When the design of an App is sort of laid out, I have build a ISInterfaceElements Class that holds stuff I will be needing around the app. (UIlabels, UIColors, UIButtons etc.) the instantiation of UIElements is just taking up space, complicating matters and if you need to change a button or color that is used in 14 places it is really bad to have written all the setup code in different places. Anyways, I usually do it like this:
With nice enums to make it easy to remember.
Because these are all Class methods I don't need to instantiate the ISInterfaceElement class to use them.
I just go:
You can build something like that for your buttons, all you need to do is build a new UIButton inside your method, set the title and action and lastly return myButton, mush like I do with the labels.
Hope it helps