简体   繁体   中英

UISegmentedControl change value programmatically from toolbar click

I have one .xib in which I have a view that has a UISegmentedControl and I am adding them programmatically.

In appdelegate I have written the code for adding toolbar and there's event.
On toolbar click the UISegmentedControl is loaded from the given mutablearray.

What I want to do is: when I click on toolbar button the value of UISegmentedControl cannot be changed.

I have written a method for adding UISegment and its value. Every time on toolbar button click, I call the method.

Method for creating UISegmentedControl:

delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;            

NSMutableArray *itemArray = [[NSMutableArray alloc] init];
itemArrayforID = [[NSMutableArray alloc] init];
itemArrayforImage = [[NSMutableArray alloc] init];

bool isfirst = true;

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
[segmentedControl removeFromSuperview];  

int temp =0;

NSLog(@"row count : %d",delegate.TopMenus.count);

for(int i=0;i<delegate.TopMenus.count;i++)
{        
    delegate.curTopsMenu = [delegate.TopMenus objectAtIndex:i];

    NSLog(@"toolbar click : %@",delegate.toolbarbtnclick);
    NSLog(@"parent_id : %@",[delegate.curTopsMenu valueForKey:@"parent_id"]);

    if([delegate.toolbarbtnclick isEqualToString:[delegate.curTopsMenu valueForKey:@"parent_id"]])
        {  
            int aaa = [[delegate.curTopsMenu valueForKey:@"top_menu_id"] intValue];
            if(isfirst)
            {
                MenuLoadID = [NSString stringWithFormat:@"%d",aaa]; 
                isfirst =false;
            }                   
            NSString *TEXT = [delegate.curTopsMenu valueForKey:@"top_menu_text"];
            NSString *TEXTID = [delegate.curTopsMenu valueForKey:@"top_menu_id"];
            NSString *SelectIMG = [delegate.curTopsMenu valueForKey:@"image_path"];

            NSLog(@" TEXT : %@ and TEXTID : %@",TEXT,TEXTID);

            [itemArray insertObject:TEXT atIndex:temp];
            [itemArrayforID insertObject:TEXTID atIndex:temp];
            [itemArrayforImage insertObject:SelectIMG atIndex:temp];                
            temp++;             
        }        
}    

segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
[segmentedControl removeFromSuperview]; 
segmentedControl.frame = CGRectMake(0, 100, 320, 40);

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;

[segmentedControl addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];

UIImage *segmentSelected = [[UIImage imageNamed:@"games-on.png"] 
 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"games-ho.png"] 
 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected 
    forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance] setBackgroundImage:segmentSelected 
    forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

[self.navigationController.view addSubview:segmentedControl];    
[segmentedControl release];   
segmentSelected = nil;

[itemArray release];

[self MenuRowCount];

Code for toolbar button click:

ViewController *VC = [[ViewController alloc] init];
[VC LoadTopMenuFromBottmClick];

Your question could be more clear, but here's my best guess at an answer. This assumes the UISegmentedControl is a property called yourSegmentedControl and has been allocated (preferably by a proper getter).

Update enabled/disabled:

[self.yourSegmentedControl setEnabled:aBOOL forSegmentAtIndex:anInt];

Update title:

[self.yourSegmentedControl setTitle:aString forSegmentAtIndex:anInt];        

That said, your code could be simpler. If you put the UISegmentedControl in the UIViewController rather than the appDelegate you can take advantage of using the toolbarItems property to attach the control to the navigation controller. This next code isn't exactly your question, but may be instructive:

@interface StocksTableViewController()
@property (nonatomic, strong) IBOutlet UISegmentedControl *stockFilterSegmentedControl;
@end

@implementation StocksTableViewController
@synthesize stockFilterSegmentedControl = _stockFilterSegmentedControl;

- (UISegmentedControl*)stockFilterSegmentedControl {
    if (!_stockFilterSegmentedControl) {
        _stockFilterSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"All",@"Holdings", @"To Do", nil]];
        [_stockFilterSegmentedControl addTarget:self action:@selector(stockFilterControlPressed:) forControlEvents:UIControlEventValueChanged];
        _stockFilterSegmentedControl.selectedSegmentIndex = 0;
        _stockFilterSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _stockFilterSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    }
    return _stockFilterSegmentedControl;
}

- (NSArray*)navFooterToolbarArray {
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.stockFilterSegmentedControl];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
    return [NSArray arrayWithObjects:flexibleSpace, barButtonItem, flexibleSpace, refresh, nil];    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.toolbarItems = [self navFooterToolbarArray];
}

Enjoy,

Damien

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM