Domanda

Pretty straight forward; just trying to change the display name of my button from "Button" to "a." But the setTitle method doesn't seem to do anything. I played around with the UIState, and that didn't seem to change much.

Do I maybe need to synthesize anything?

[Button1 setTitle:@"a" forState:UIControlStateNormal];

EDIT: I did an NSLog, and the button is showing as NULL, which I've been told means it isn't connected properly. But I double-checked my Connections Inspector, looked at the H file, and everything seems to link up fine. (http://imgur.com/EFsp51U) What am I missing?

H file and Connections Inspector

È stato utile?

Soluzione 2

I went digging through my code, and in my viewDidLoad method, I had the following:

Button1 = NO;

I think I was trying to type out "selected" but got distracted and didn't come back to it, and so was inadvertently turning my own button off. The correct code is:

self.Button1.selected=NO;

[self.Button1 setTitle:@"a" forState:UIControlStateNormal];

Altri suggerimenti

If you have created your button in Interface Builder, then go there and change its type to custom.

enter image description here

And also check if you have connected your IBOutlet correctly.

  • Make sure Button1 is not nil
  • Make sure Button1.hidden = NO;

I'm not sure that the problem is about thread but you may want to try to set the title on main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [Button1 setTitle:@"a" forState:UIControlStateNormal];
});

Your code is perfectly valid. But just to make sure everything is ok, add following extra line,

Button1.selected = NO;
[Button1 setTitle:@"a" forState:UIControlStateNormal];

Make sure you have synthesized your Button.

//this assumes you have a button "myButton" declared in your interface file
@implementation
@synthesize myButton;

-(void) myMethod{
    [myButton setTitle:@"The new Title" forState:UIControlStateNormal];
}
@end

You should have your button connected through IB outlets to your .h file or create the button entirely through code.

Also it is good practice to not have variable names beginning with a capital letter.

If you haven't explicitly synthesized it then you should be calling either

[_Button1 setTitle:@"a" forState:UIControlStateNormal];

or

[self.Button1 setTitle:@"a" forState:UIControlStateNormal];

Do the following:

  1. Do

    NSLog(@"%@", Button1);
    

    and check that it's not nil.

  2. Check that

    NSLog(@"%d", btn.state);
    

    should return 0.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top