How to pass and store information through methods
In the following example when the screen in touched a method is called that passes a number to the view controller that is stored by MyViewController. Only problem the view controller doesn开发者_运维问答't store the number since I want to use it later in another method. Please help. Thank you.
MyViewController.h
@class MyView;
@interface ViewController : UIViewController {
int number;
}
-(void)assignNumber(int)sentNumber;
@property int number;
MyViewController.m
-(void)loadView {
MyView *aView = [[MyView alloc] initWithFrame:wholescreen];
self.view = aView;
[aView release];
}
-(id)initWithNibName...... {
number = 0;
}
-(void)assignNumber:(int)sentNumber {
number = sentNumber;
}
MyView.h & MyView.m
Default with TouchesBegin, when touched [viewController assignNumber:5];
If you don't declare @synthesize, you won't be able to access number from outside of the viewController internals. Try adding
@synthesize number;
to the top of MyView.m; This creates setters and getters. then, you can replace your
[viewController assignNumber:5];
call with
[viewController setNumber:5];
and access it with either viewController.number
or [viewController number]
.
You may want to @synthesize the variable number in your ViewController implementation.
精彩评论