Iphone 以编程方式添加UIBarButtonim或编辑通过IB插入的UIBarButtonim时出现问题
我正在做一个项目,它使用底部的一个选项卡栏来浏览我的应用程序的5个部分。其中一个部分加载一个地图视图,然后生成一个web视图。起初是用圆形矩形按钮完成的,但它们妨碍了视图的显示,所以我决定切换到导航栏。在Interface Builder中,我插入了一个导航项,并在其中插入了两个栏按钮项(左侧和右侧)。我已经获得了两个按钮的功能,通过从文件所有者拖动控件来触发与每个按钮关联的方法。这很好,可以启动我的web视图,但我希望能够做到的是,在web视图中将其中一个按钮更改为“完成”或“返回”按钮一次,到目前为止,我还没有弄清楚如何做到这一点。我尝试过以编程方式创建这两个按钮,但都没有出现。这是我用来完成这项任务的代码Iphone 以编程方式添加UIBarButtonim或编辑通过IB插入的UIBarButtonim时出现问题,iphone,uibarbuttonitem,uinavigationitem,Iphone,Uibarbuttonitem,Uinavigationitem,我正在做一个项目,它使用底部的一个选项卡栏来浏览我的应用程序的5个部分。其中一个部分加载一个地图视图,然后生成一个web视图。起初是用圆形矩形按钮完成的,但它们妨碍了视图的显示,所以我决定切换到导航栏。在Interface Builder中,我插入了一个导航项,并在其中插入了两个栏按钮项(左侧和右侧)。我已经获得了两个按钮的功能,通过从文件所有者拖动控件来触发与每个按钮关联的方法。这很好,可以启动我的web视图,但我希望能够做到的是,在web视图中将其中一个按钮更改为“完成”或“返回”按钮一次,
UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:@"Update Position"
style:UIBarButtonItemStylePlain target:self action:@selector(findMe)];
self.navigationItem.leftBarButtonItem = updatePosition;
[updatePosition release];
当我运行应用程序时,该按钮不会显示在我使用Xcode插入的导航项上。我能让按钮出现的唯一方法是像我前面提到的那样,用Interface Builder插入按钮。但是,一旦我打开了新视图,我就不能将左侧按钮更改为“后退”或“完成”按钮。我试着运行这段代码来改变我用IB插入的UIBarButtonItem上按钮的样式
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
但是没有用。这里有一些实用方法可以帮助您:
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
UIImage *buttonImage = [UIImage imageNamed:imageName];
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
style:UIBarButtonItemStyleBordered
target:sender
action:action] autorelease];
return buttonItem;
}
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
target:sender
action:action] autorelease];
return buttonItem;
}
用法:
// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:@selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];
因此,我看到两个方法的标题都是setRightBarButtonim,但是调用该方法的代码行将其分配给了LeftBarButtonim。我假设上述方法是可互换的,因为它们都只返回一个按钮项,所以它被分配到哪一方无关紧要?我一直在尝试实现这一点,但我没有真正得到您在这里使用的“实用程序”保留字。对于Obj C来说是个新手,所以如果这是常识,我很抱歉。如果你不喜欢,你不必使用实用类。您只需将这些方法(替换为-)放在视图控制器实现中,并调用[self-SetRightBarButtonim:];相反,你是对的,当我想纠正巴布托主义时,我偶然发现了左巴布托主义。你说得对,这些方法也可以互换。如果您真的愿意,您可以简单地创建另一个方法并将其称为setLeftRightBarButtonItem。