views:

49

answers:

1

I'm trying to get a UI Button to interact with a UI Label through Xcode and Interface Builder. What should I change in this code to do so? (I have everything linked up in Interface Builder already. The app just crashes when I press the button.)

@synthesize window;
@synthesize label;
@synthesize anotherLabel;
@synthesize myButton;


    #pragma mark -
    #pragma mark Application lifecycle



    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

     [myButton setTitle:@"Press Here" forState:UIControlStateNormal];

     window.backgroundColor = [UIColor grayColor];

     label.text = [[NSDate date] description];


        // Override point for customization after application launch.

     [myButton addTarget:anotherLabel action:@selector(doButton:) forControlEvents:UIControlEventTouchUpInside];

        [window makeKeyAndVisible];

     return YES;
    }

     -(void) doButton:(UILabel *)anotherLabel{
     static int count;
        count++;

            }
+1  A: 

Ok, first off, your not updating the label this way, since just passing anotherLabel as a parameter and that will not mean that it will change the properties of it. Also, you are not passing the correct parameter for doButton:. In this case I would just forget a parameter and up date the anotherLabel by saying:

static int count;
count++;
NSString *countString = [NSString stringWithFormat:@"%d", count];
[anotherLabel setText: countString];
thyrgle
Thanks for the response! I seem to get an error when I replace the NSString. "Conflicting types for 'count.' Do you happen to know what's causing the error?
thisislev
`static int count;count++;NSString *countStr = [NSString stringWithFormat:@"%d", count];[anotherLabel setText: countStr];`
Emil
would probably work better. (string name was same as int name)
Emil
_I hate that the comment code highlighting sucks really bad.._
Emil
@thisslev: Sorry, updated my answer.
thyrgle
@Emil: You mean: `static int count; count++; NSString *countStr = [NSString stringWithFormat:@"%d", count]; [anotherLabel setText: countStr];`
thyrgle
@thyrgle That's exactly what I wrote...
Emil
@Emil: I rewrote with formatting.
thyrgle
@thyrgle After importing the lines of code into Xcode, the app finally started launching. The only problem is, it crashes after I press the button. What am I doing wrong?
thisislev