第一種:
UIImage *searchimage=[UIImage imageNamed:@"search.png"];
UIBarButtonItem *barbtn=[[[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)] autoRelease];
barbtn.image=searchimage;
self.navigationItem.rightBarButtonItem=barbtn;
這種設置出來的item圖片跟大小是固定的
其隱藏方法是:在需要隱藏的時候self.navigationItem.xxxItem = nil;
顯示方法是重新alloc-init一次;
第二種:
IButton*rightButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,30,30)];
[rightButtonsetImage:[UIImageimageNamed:@"search.png"]forState:UIControlStateNormal];
[rightButtonaddTarget:selfaction:@selector(searchprogram)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*rightItem = [[UIBarButtonItemalloc]initWithCustomView:rightButton];
[rightButton release];
self.navigationItem.rightBarButtonItem= rightItem;
[rightItem release];
這種方式設計出來的item比較靈活,尤其是在 隱藏顯示的時候:
隱藏方法:self.navigationItem.xxxItem.customView.hidden =YES;
顯示方法,同上xxx.hidden = NO;
這種方式更合理一些,因為不需要不斷的創建/消除,所以推薦用這種方法!
第三種:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ];