1

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

Michelle
  • 365
  • 1
  • 4
  • 18
  • 1
    Are you sure your UIButton outlet is wired up correctly for Button1? – Kevin DiTraglia May 07 '14 at 03:02
  • In the Connections Inspector, when I hover over the Button1 connector, it highlights the appropriate button. My H file shows the Button1 IBOutlet as being connected. Is there another way to check besides those two? – Michelle May 07 '14 at 03:04
  • It's in a Void method, within a switch statement, inside the ViewController.m file. – Michelle May 07 '14 at 03:28
  • Can you please write more code? As what you are telling seems quite difficult to happen ever - otherwise you are making some else mistake. Because the line of code that you have written is perfectly valid. – Arslan Ali May 07 '14 at 04:19
  • Please verify that you have not setImage to that UIButton – Rahul Patel May 07 '14 at 04:41

8 Answers8

6

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.

Community
  • 1
  • 1
iBug
  • 2,334
  • 3
  • 32
  • 65
  • I did build the button through the Interface Builder, but changing to custom didn't seem to correct the issue. I double-checked the outlet, and it seems everything is wired up. Are there any potential issues for having straight IBOutlets instead of properties in the H file? – Michelle May 07 '14 at 22:28
  • In this case, it should work. Your IBOutlets are connected correctly. Its weird that title is not set. – iBug May 08 '14 at 06:42
  • Weird, mine was set to System. Changing it to Custom worked. What's the difference again? – Chucky Jul 05 '21 at 10:30
  • here is a detailed answer for difference between custom and system buttons. https://stackoverflow.com/a/47468314/3081929 – iBug Jul 05 '21 at 15:18
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];
Michelle
  • 365
  • 1
  • 4
  • 18
0
  • Make sure Button1 is not nil
  • Make sure Button1.hidden = NO;
akin
  • 183
  • 2
  • 13
0

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.

Nic Robertson
  • 1,198
  • 9
  • 26
0

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];
});
Envil
  • 2,687
  • 1
  • 30
  • 42
0

Do the following:

  1. Do

    NSLog(@"%@", Button1);
    

    and check that it's not nil.

  2. Check that

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

    should return 0.

Pang
  • 9,564
  • 146
  • 81
  • 122
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • When I do option 1 and set a breakpoint right after, Button1=(UIButton *) nil. The NSLog itself shows (null) Am I reading it right that it's not getting that string assigned? But why? (The 2nd option doesn't return anything at all.) – Michelle May 07 '14 at 22:55
  • Have you taken the Button in the XIb then check in the IBOutlet connection. Check you have not set Button1=nil in your code – Sunny Shah May 08 '14 at 04:10
0

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];
Rukshan
  • 7,902
  • 6
  • 43
  • 61
0

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];

Evan
  • 750
  • 7
  • 13
  • I'm not creating the buttons as properties, but putting them between {} under the @interface in the H file. When I try to do self or _, I get an error. Should I still be synthesizing them? – Michelle May 07 '14 at 22:34
  • @Michelle I think best practice would be to declare them as properties. Not to say that this is the problem. NSHipster.com has a good article on it this week. To quote `As with anything in modern Objective-C, properties are preferred to direct ivar access. The same is true of IBOutlets:` http://nshipster.com/ibaction-iboutlet-iboutletcollection/ – Evan May 07 '14 at 23:10
  • 1
    In modern objective c properties will be automatically synthesized for you. – Evan May 07 '14 at 23:12