Iphone 如何以编程方式更改UIBarButtonim的文本?
我读过这篇文章: 但它似乎不适用于我的自动隐藏工具栏? 我还尝试了barbuttonItem.title来设置文本,但也失败了Iphone 如何以编程方式更改UIBarButtonim的文本?,iphone,objective-c,Iphone,Objective C,我读过这篇文章: 但它似乎不适用于我的自动隐藏工具栏? 我还尝试了barbuttonItem.title来设置文本,但也失败了 有什么想法吗?您正在界面生成器中创建按钮吗?确保标识符设置为“自定义”,否则将无法更改标题 在Interface Builder中选择UIBarButtonItem,打开inspector命令+Shift+I,然后在Identifier旁边的下拉列表下选择Custom。我认为我的问题与您的问题类似,但不完全相同:我希望附件工具栏上的文本反映最后一个选择器组件的选择 但
有什么想法吗?您正在界面生成器中创建按钮吗?确保标识符设置为“自定义”,否则将无法更改标题
在Interface Builder中选择UIBarButtonItem,打开inspector命令+Shift+I,然后在Identifier旁边的下拉列表下选择Custom。我认为我的问题与您的问题类似,但不完全相同:我希望附件工具栏上的文本反映最后一个选择器组件的选择 但是,我无法更改添加到UIBarButtonim的UILabel的文本,该UIBarButtonim是在多组件滑动UIPickerView上显示的inputAccessoryView的一部分,用于多个数据输入UIExtFields中的每一个。这些元素是以编程方式创建的Xcode v 4.3 4E109,但文本字段除外,每个文本字段都是在情节提要中创建并声明为属性的 将文本指定给toolbarTitle标签,然后将toolbarTitle作为自定义视图分配/初始化工具栏按钮项:
toolbarTitle.text = @"myTextFromPickerComponent";
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:toolbarTitle];
//nor alloc/init-ing the bar button item directly, passing the new text to initWithTitle:
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:toolbarTitle.text style:UIBarButtonItemStyleDone target:self action:nil];
给出期望的行为;i、 例如,标签文本没有改变,尽管我向NSLog确认我确实发送了新文本
我从数据输入、输入视图和InputAccessory视图的自定义视图下的文档中得到了一个提示:
第一个响应程序可以通过调用UIResponder的reloadInputViews方法来重新加载输入视图和附件视图
我通过让当前的UITextField调用reloadInputViews实现了这一点,这对我来说适用于上面给出的两个代码片段中的任何一个。尝试以下方法:
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(yourMethod:)];
为UIBarButtonItem创建一个IBOutlet,本例中为outToMyButton
合成in.m
outToMyButton.title=[NSString stringWithFormat:@%i,myInt];//或任何NSString初始化
以编程方式更改UIBarButtonim的标题:
@interface YourController : UIViewController
{
UIBarButtonItem *barButton;
}
- (void)viewDidLoad
{
barButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Set", @"Set")
style:UIBarButtonItemStylePlain
target:self
action:@selector(barButtonClicked)];
}
巴布托主义行动:
问题是以编程方式更改uibarbuttonim的文本: 这就是你如何做到的。 一个例子是把天气信息放在酒吧里
import UIKit
...
@IBOutlet weak var weatherBarLabel: UIBarButtonItem!
//then
func setTheWeatherInfo(weather: WeatherModel){
weatherBarLabel.title = "\(weather.temperature)°"
}
设置为Custom是否意味着initWithCustomView?如果是,则我的项目已设置为自定义。如下所示:百分比=[[UILabel alloc]init];percentage.text=@HELLO;UIBarButtonItem*percentageBarItem=[[UIBarButtonItem alloc]initWithCustomView:percentage];我想在将其放在工具栏上后保持其可更改性。抱歉,我不知道如何在注释中弥补…/\n如果您只是使用文本,我建议调用-idinitWithTitle:NSString*标题样式:UIBarButtonimStyle目标:idtarget操作:SELaction,然后您可以稍后轻松更改标题。谢谢!它工作得很好。我能再问一个问题吗?如何在相同条件下更改UIBarButtonim滑块的值,即自动隐藏工具栏感谢您的回答:调用ReloadInputView。
import UIKit
...
@IBOutlet weak var weatherBarLabel: UIBarButtonItem!
//then
func setTheWeatherInfo(weather: WeatherModel){
weatherBarLabel.title = "\(weather.temperature)°"
}