How to use -(id)init?!

lukelee

macrumors newbie
Original poster
Apr 20, 2011
3
0
Sydney
I just want to initialize NumberOfSides to 5! and display it. just can't get it right!

.h
Code:
@interface HelloPolyViewController : UIViewController {
	IBOutlet UILabel *NumberOfSidesLabel;
	IBOutlet UIButton *decrease;
	IBOutlet UIButton *increase;
	int NumberOfSides;

}
@property(nonatomic,retain)IBOutlet UILabel *NumberOfSidesLabel;
@property int NumberOfSides;

-(IBAction)decrease:(id)sender;
-(IBAction)increase:(id)sender;
- (id)init;
.m
Code:
#import "HelloPolyViewController.h"
@implementation HelloPolyViewController
@synthesize NumberOfSidesLabel;
@synthesize NumberOfSides;

- (id)init {
	if (self=[super init]) {
		NumberOfSides = 5;
	}
}


-(IBAction)decrease:(id)sender
{
	NumberOfSidesLabel.text = [NSString stringWithFormat:@"%d",NumberOfSides];
	NSLog(@"123 %d",NumberOfSidesLabel);
	
}
-(IBAction)increase:(id)sender {
	//
}
 

Jeffrey Lim

macrumors newbie
Sep 9, 2003
17
3
I just want to initialize NumberOfSides to 5! and display it. just can't get it right!

-(IBAction)decrease:(id)sender
{
NumberOfSidesLabel.text = [NSString stringWithFormat:mad:"%d",NumberOfSides];
NSLog(@"123 %d",NumberOfSidesLabel);
[/CODE]
Use:

NSLog(@"123 %d", NumberOfSides);

or:

NSLog(@"123 %@", NumberOfSidesLabel);
 

orangetripwire

macrumors newbie
Feb 24, 2009
7
0
Code:
- (id)init {
	if (self=[super init]) {
		NumberOfSides = 5;
	}
      [I]  return self;[/I]
}
You need to return something in the method since you have - (id) init and not something like - (void) init. Note that the init method must return an - (id), so - (void) init is not a valid init method.

Also like said above %d is for something like an int, %@ is for something like a string in the NSLog statement.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.