2012-03-13 13 views
1

iOSのNavigationBarに1つのボタンと1つのラベルを挿入したいとします。ナビゲーションバーに複数のコントロールを追加するにはどうすればよいですか?

私はUISegmentedControlで試してみましたが、1つのコントロールで完全に正常に動作します!

今問題は、私が前に言ったように複数のコントロールを追加したいのです。どうすればいいですか?

私のコード

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); 

を見て、それがiPhone上のナビゲーションバーのみだけ左右のバーボタンアイテム、およびタイトルビューをサポートしてくれEXC_BAD_ACCESS

答えて

0

のエラーが発生します。 iPadの(より大きい)ナビゲーションバーのみが任意の数のボタンを許可します。

ナビゲーションコントローラなしでナビゲーションバーを使用している場合は、必要なものをサブビューとして表示できます。

+0

私は、コードの束を追加したしてください、私の質問を参照してください! – Chintan

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ではないので、これはある

[v insertSubview:listingsLabel atIndex:0]; 

で何をすべきかわからないので、それがクラッシュしますオブジェクトはまだありません。 (アークを使用していない場合)ので

UIView *v; 

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

を変更して再度ここにそれを解放

self.navigationItem.titleView = v; 
[v release]; 
関連する問題