UIbarbuttonItem 在storyboard中如何关联action等

来源:互联网 发布:ubuntu设置语言为中文 编辑:程序博客网 时间:2024/05/17 03:49

1. 测试结果就是诡异啊:

UIBarButtonItem assigned action in Storyboard but not executing

http://stackoverflow.com/questions/9435105/uibarbuttonitem-assigned-action-in-storyboard-but-not-executing//

http://pastebin.com/45BiK1GR  这个家伙也说关联不work。。。

UIBarButtonItem does not divide actions in touch up inside and so on. It only has a click or selector, something like that

结论就是你最多就是可以知道按下没?如何关联selector? 不分touchup inside/outside了。
2. 在一组uibarbuttonitme中,有一个好像什么多没有设置,就可以关联到一个action,我靠。。。肯定是在storyboard设么地方搞了下。虽然我们选者了custome的controller class,估计还是通过storyboard和custom class组合成了一个controller。《这也能解释为啥自己使用的navigationcontroller是不能产生某类relation segue的》

    后来就设置了sent actions关联到一个action 函数


那么我们来看建议:
1.通过segue

If the button will always take you to the next view (that is, there is no validation that needs to be performed on the current view before you can leave) then you can call your function fromprepareForSegue:. Use segue.identifier to work out which segue you are dealing with.
2. 尝试通过IB中的send actions来设置
3.  代码来控制了;

barItemUndo.action=@selector(undo);


============整理关于使用UInavigationcontroller ========

// Override point for customization after application launch.    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];        self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];     [self.window addSubview:navController.view]; //把自己的view设置上了

为什么类似以上的代码产生的

UINavigationController能够在一些场景下很好使用?某些情况无法使用?

最好使用的就是有self。window的方式,以及addviewcontrollers方法中做参数 。如果只是一个nib,然后给他一个uinavigationcontroller,就有点麻烦。。。 以上的方面也就是把自己包了下,然后让windows加载了所谓的《rootviewcontroller》 这个要在IB中设置好像还不行呢!!!!!
initWithRootViewController
self.navigationItem.title = @"标题";

运行:

界面美工真是 ----无穷经,而来如此弄潮儿

在viewDidLoad方法中,去掉self.navigationItem.title = @"标题";,并添加代码:

//自定义标题titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明titleLabel.font = [UIFont boldSystemFontOfSize:20];  //设置文本字体与大小titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1];  //设置文本颜色titleLabel.textAlignment = UITextAlignmentCenter;titleLabel.text = @"自定义标题";  //设置标题self.navigationItem.titleView = self.titleLabel;

运行:


self.navigationItem// //// title::::   titleView:::: (万能uiview接收器???)//左右按钮代码 //setbackgroundImage
setBackgroundImage

leftBarButtonItem设置的代码:

self.navigationItem.leftBarButtonItem = (UIBarButtonItem *)self.navigationItem.leftBarButtonItems = (UIBarButtonItem *)self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *)self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *) animated:(BOOL)self.navigationItemsetLeftBarButtonItems:(NSArray *)self.navigationItemsetLeftBarButtonItems:(NSArray *) animated:(BOOL)
//添加左按钮UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]                                          initWithTitle:@"左按钮"                                          style:UIBarButtonItemStylePlain                                          target:self                                           action:@selector(myAction)];[self.navigationItem setLeftBarButtonItem:leftButton];
[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)

用了系统自带的按钮样式,这些样式的标签和效果如下:《我觉得这个图不错,直观!》 其中有个 systemItemPageCurl只能在tool bar上显示的

                      标签      效果                       标签         效果UIBarButtonSystemItemAction            UIBarButtonSystemItemPause        UIBarButtonSystemItemAdd            UIBarButtonSystemItemPlay        UIBarButtonSystemItemBookmarks            UIBarButtonSystemItemRedo        UIBarButtonSystemItemCamera            UIBarButtonSystemItemRefresh        UIBarButtonSystemItemCancel            UIBarButtonSystemItemReply        UIBarButtonSystemItemCompose            UIBarButtonSystemItemRewind        UIBarButtonSystemItemDone            UIBarButtonSystemItemSave        UIBarButtonSystemItemEdit            UIBarButtonSystemItemSearch        UIBarButtonSystemItemFastForward            UIBarButtonSystemItemStop        UIBarButtonSystemItemOrganize            UIBarButtonSystemItemTrash        UIBarButtonSystemItemPageCurl            UIBarButtonSystemItemUndo        

设置Navigation Bar背景图片

首先将准备好作为背景的图片拖到工程中,我用的图片名称是title_bg.png。

将上面的代码改成:

//设置Navigation Bar背景图片UIImage *title_bg = [UIImage imageNamed:@"title_bg.png"];  //获取图片CGSize titleSize = self.navigationController.navigationBar.bounds.size;  //获取Navigation Bar的位置和大小title_bg = [self scaleToSize:title_bg size:titleSize];//设置图片的大小与Navigation Bar相同[self.navigationController.navigationBar      setBackgroundImage:title_bg     forBarMetrics:UIBarMetricsDefault];  //设置背景


原创粉丝点击