editButtonItem in a segmented control?

Discussion in 'iOS Programming' started by John Baughman, Oct 17, 2009.

  1. John Baughman macrumors regular

    Oct 27, 2003

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    I have an edit button in my navigation bar that works great but I would like to put it in a segmented control with an Add button. Is this possible?

    As far as I can tell, the answer is no.


  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Yes, it's possible, via the customView property of UIBarButtonItem. Check the NavBar sample app for how you might do this.
  3. John Baughman thread starter macrumors regular

    Oct 27, 2003
    I have successfully created a 2 button segmented control like this...

    segmentedControl = [ [ UISegmentedControl alloc ] initWithItems: nil ]; 
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
    [ segmentedControl insertSegmentWithTitle: @"Prefs" atIndex: 0 
    										 animated: NO ]; 
    [ segmentedControl insertSegmentWithTitle: @"Add" atIndex: 1 
    										 animated: NO ]; 
    [ segmentedControl addTarget: self action: @selector(segmentedControlPressed:) 
    		forControlEvents:UIControlEventValueChanged ];
    UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    self.navigationItem.rightBarButtonItem = segmentItem;
    [segmentedControl release];
    [segmentItem release];
    What I want to do is replace the Pref button at index 0 with an Edit button created with self.editButtonItem, ie with all the default behavior of an Edit button. The only way I can find to add items to the segment is with title, image, or items (array with images or titles).


Share This Page