Search code examples
iosundeclared-identifieruisegmentedcontrol

Change button text inside segment control


I have segment control and code like this in .m file

-(IBAction)sectionswitch:(id)sender {

    if (control.selectedSegmentIndex == 0) {
        UIImage *dekabristov = [UIImage imageNamed:@"dekabristov.png"];
        [image setImage:dekabristov];
    }

    if (control.selectedSegmentIndex == 1) {
        UIImage *fabrika = [UIImage imageNamed:@"fabrika.jpg"];
        [image setImage:fabrika];
    }

}

How i can change title of action button inside segment control? If i write [button setTitle:@"Button!"]; Xcode says "Use undeclared identifier "button", but -(IBAction)button:(id)sender; in .h file


Solution

  • in that way, you have not declared a button, but only an action called 'button'.

    in your .h file you should do:

     @interface yourViewController : UIViewController {
    
       UIButton *button;
    
     }
    

    and for change title of segment control you can do:

     [yourSegmentControl setTitle:@"Button!" forSegmentAtIndex:0]; //0 is the first
    

    if you want change a title of an UIButton:

    [button setTitle:@"Title" forState: UIControlStateNormal];