2012-03-13 37 views
1

我想在iOS的NavigationBar中插入一个按钮和一个标签。如何在导航栏中添加多个控件?

我试图与UISegmentedControl和它的工作原理完全罚款与一个控制!

现在的问题是我想之前我怎么能为我的话可以添加多个控件?

看我的代码

UIView *v; 
[v insertSubview:listingsLabel atIndex:0]; 
[v insertSubview:shareBtn atIndex:1]; 

[v setFrame:[self.navigationController.toolbar bounds]]; 
self.navigationItem.titleView = v; 
v.frame = CGRectMake(0, 0, 200, 29); 

,这让我的EXC_BAD_ACCESS

回答

0

错误在iPhone上导航栏仅支持左右栏按钮项目,一个标题视图。只有iPad上的(大)导航栏允许任意数量的按钮。

如果您使用的是导航栏没有导航控制器,我想你可以只是扑通无论你想在它的子视图。

+0

请参阅我的问题请我加入了一串代码! – Chintan 2012-03-13 13:17:33

0
UIButton *btnBack = [[UIButton alloc]initWithFrame:CGRectMake(5,5,60,32)]; 
    [btnBack setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal]; 
    btnBack.backgroundColor = [UIColor clearColor]; 
    [btnBack addTarget:self action:@selector(eventBack:) forControlEvents:UIControlEventTouchUpInside]; 
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(12, 2, 60,25)]; 
    [lbl setBackgroundColor:[UIColor clearColor]]; 
    lbl.font = [UIFont fontWithName:@"Helvetica" size:12]; 
    lbl.font = [UIFont boldSystemFontOfSize:12]; 
    lbl.textColor = [UIColor whiteColor]; 
    lbl.text [email protected]" Back"; 
    [btnBack addSubview:lbl]; 
    [lbl release]; 

    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack]; 
    self.navigationItem.leftBarButtonItem = backBarBtn; 
    [btnBack release]; 
    [backBarBtn release]; 


    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 170, 40)]; 
    lblTitle.text = @"ABC"; 
    lblTitle.backgroundColor = [UIColor clearColor]; 
    lblTitle.textColor = [UIColor whiteColor]; 
    lblTitle.textAlignment = UITextAlignmentCenter; 
    lblTitle.font = [UIFont fontWithName:@"Helvetica" size:17]; 
    lblTitle.font = [UIFont boldSystemFontOfSize:17]; 
    self.navigationItem.titleView = lblTitle; 
    [lblTitle release]; 
1
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44.01)]; 

// create the array to hold the buttons, which then gets added to the toolbar 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

// create a standard "add" button 
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]]; 
[segmentedControl insertSegmentWithTitle:@"All" atIndex:0 animated:NO]; 
[segmentedControl insertSegmentWithTitle:@"Related" atIndex:1 animated:NO]; 
segmentedControl.selectedSegmentIndex = 0; 
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
[segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; 
// create a standard "add" button 
UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// create a spacer 
bi = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
[buttons addObject:bi]; 
[bi release]; 

// create a standard "refresh" button 
bi = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// stick the buttons in the toolbar 
[tools setItems:buttons animated:NO]; 

[buttons release]; 

// and put the toolbar in the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 
1

因为你不初始化UIView的正确的方式,它崩溃,因为iPhone不知道做什么用

[v insertSubview:listingsLabel atIndex:0]; 

这是做,因为v是不是对象呢。因此,改变

UIView *v; 

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 29)]; 

在这里再次释放(如果不使用弧)

self.navigationItem.titleView = v; 
[v release]; 
相关问题