Как добавить несколько элементов управления на панели навигации? - PullRequest
1 голос
/ 13 марта 2012

Я хочу вставить одну кнопку и одну метку в панель навигации в iOS.

Я пробовал с 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

Ответы [ 4 ]

1 голос
/ 13 марта 2012

Из-за неправильной инициализации 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];
1 голос
/ 13 марта 2012
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];
0 голосов
/ 13 марта 2012
    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 =@"  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];
0 голосов
/ 13 марта 2012

Панель навигации на iPhone поддерживает только элементы кнопок левой и правой панели и вид заголовка. Только (большая) панель навигации на iPad допускает произвольное количество кнопок.

Если вы используете панель навигации без контроллера навигации, я полагаю, вы можете просто добавить на нее все, что вы хотите, в качестве подпредставлений.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...