iOS:UINavigation按钮

时间:2012-12-24 09:30:45

标签: ios6 uinavigationbar uinavigationitem

我正在尝试将UINavigationController中的导航按钮显示为未显示,但导航栏可见!代码如下,

leftbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbtn setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[leftbtn addTarget:self action:@selector(hom) forControlEvents:UIControlEventTouchUpInside];
[leftbtn setImage:[UIImage imageNamed:@"home.png"] forState:UIControlStateNormal];
UIBarButtonItem *random = [[UIBarButtonItem alloc] initWithCustomView:leftbtn];
rigtbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rigtbtn setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[rigtbtn addTarget:self action:@selector(edity) forControlEvents:UIControlEventTouchUpInside];
[rigtbtn setImage:[UIImage imageNamed:@"edite.png"] forState:UIControlStateNormal];
UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:rigtbtn];
navigationController.navigationItem.leftBarButtonItem=random;
navigationController.navigationItem.rightBarButtonItem=random1;

这段代码出了什么问题?请帮我解决

2 个答案:

答案 0 :(得分:1)

除了设置导航控制器的导航项外,您还必须设置其根视图控制器的属性(如果您的代码是这样组织的,可能是self)。因此,如果rootViewController是您启动导航控制器的视图控制器,则必须写下:

rootViewController.navigationItem.leftBarButtonItem = random;
rootViewController.navigationItem.rightBarButtonItem = random1;

答案 1 :(得分:0)

而不是,

navigationController.navigationItem.leftBarButtonItem=random;
navigationController.navigationItem.rightBarButtonItem=random1;

使用,

self.navigationItem.leftBarButtonItem=random;
self.navigationItem.rightBarButtonItem=random1;

您无需创建或访问UINavigationController来设置导航栏上的栏按钮项目。如果您正在按当前视图控制器,则可以使用上面的代码来设置这些按钮。推送的视图控制器已具有设置此属性的属性。