views:

277

answers:

4

I am unsure why this code will not work and what i want it to do is when i click a button(action: buttonclick) i want it to change the two text box's(MyTextLabel & MyTextLabel2) text increment the value "r" by one. here is the code:

MainView.h


#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {
    IBOutlet UIButton *MyButton;
    IBOutlet UILabel *MyTextLabel;
    IBOutlet UILabel *MyTextLabel2;

}

@property (nonatomic, retain) UIButton *MyButton;
@property (nonatomic, retain) UILabel *MyTextLabel;
@property (nonatomic, retain) UILabel *MyTextLabel2;


- (IBAction)buttonclick;
@end

MainView.m:

#import "MainView.h"
#include <stdlib.h>
#include <libc.h>


@implementation MainView

@synthesize MyButton, MyTextLabel, MyTextLabel2;

int r;  


- (IBAction)buttonclick {

    r++

 if(r < 50) {

  MyTextLabel.text = @"< 50";
 } else {
  MyTextLabel2.text = @"=> 50";
 }

}
@end
A: 

You don't say in your question what, exactly, isn't working the way you expect it. My guess is going to be that your outlets aren't actually connected properly. You might want to add a log statement to buttonClick like this:

NSLog(@"button click called! MyTextLabel is %@", MyTextLabel);

The point being, mostly, to be sure it isn't nil.

Sixten Otto
Sorry, for not telling! The code stalls and stops on opening the app and in the code it stops on the "@synthesize"
Can you add the stack trace in the original question?
nall
+1  A: 

I have a feeling something's wrong with the way you're using int r. Try putting static int r; at the top of the @interface line in MainView.h and also add, under the - (IBAction)buttonclick; line:

+(void) initialize;

Then remove int r; from MainView.m. Then in MainView.m add:

+(void) initialize {
   count = 0;
}
Jorge Israel Peña
Or just make r a real ivar: add it to the variables declared in the @interface, initialize it to 0 in -init, and then increment it in buttonClick.
Sixten Otto
Oh, that's another way of doing it :)
Jorge Israel Peña
@Sixten Otto: you don't need to use initialize r. It is set to 0 in the alloc.
Abizern
@Abizern I just knew someone was going to jump on that. :-) Yes, the runtime will zero out ivars when allocating the memory, so that part isn't strictly necessary.
Sixten Otto
Thanks for the help but now it says "expected special-qualifier-list before static" under the "#import "MainView.h" line. it also says that "r" is still undeclared!
YAY! I finally figured it out! all I had to do was declare "int r" in the @property line! ahh im such an idiot in objective-c!
+1  A: 

I see two issues:

  1. You can't declare int r where you have it. You should declare it in your interface's variable block (where you declare your button and labels or outside a method) or in the method definition.
  2. The line with r++ isn't ended with a semi-colon.
Martin Gordon
A: 

In Objective-c the names of variables and properties must start with a lowercase letter to conform to the Key-Value Coding (KVC) protocol (always myButton, never MyButton). The @synthesize directive relies on this to generate the setters and getters. Thus, for myButton @synthesize will generate -(void)setMyButton:(UIButton *)button and -(UIButton *)myButton. So, lowercase MyButton and its colleagues and see if it helps.

Elise van Looij