Quote:
Originally Posted by vogueestylee
Hi, I would like to pass parameter but not use NSUserDefaults but just some like
in ViewController.m
Code:
-(IBAction)about:(id)sender{
AboutView *screen = [[AboutView alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
and AboutView.m is
Code:
@implementation AboutView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
I would like to have something like this:
in ViewController.m
Code:
-(IBAction)about:(id)sender{
AboutView *screen = [[AboutView alloc] initWithNibName:nil bundle:nil parameter:1];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
and AboutView.m is
Code:
@implementation AboutView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parameter:(int a)
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
if (a == 1) { do staff...}
}
but It just don't work.. I have bad syntax.. how to make it correct?
thank you!
|
You can't pass a parameter to a nib file. However, you can set up the object that you create from the nibfile (your AboutView view controller) with a property, and assign that property before invoking the view controller:
Code:
-(IBAction)about:(id)sender{
AboutView *screen = [[AboutView alloc] initWithNibName:nil bundle:nil];
screen.someProperty = valueToPass;
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
Then, in your aboutView's viewDidLoad method, take the value of someProperty and do whatever you need to do with it (install it in a label, use it as a pathname to load a file from disk, whatever.)
Edit: I just reread your post, and think I understand what you are trying to do. In general, I'd suggest the approach I suggested aove. It's clean and simple to implement. If you really need a parameter at init time, though, you can create a custom initializer for your view controller that takes an extra parameter, as you describe:
Code:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle: (NSBundle *) nibBundleOrNil
parameter:(int) a;
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization goes here...
if (a == 1)
{
//do stuff...
}
return self;
}
}
And call it just the way you outlined
Code:
-(IBAction)about:(id)sender{
AboutView *screen = [[AboutView alloc] initWithNibName:nil
bundle:nil
parameter:1
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
That approach has potential problems, however. If you instantiate one of your AboutView view controllers directly in a nib file, your custom init method won't get called. The same goes for invoking it from a segue if you're using storyboards.
you should really override the standard initWithNibName:bundle: method and make it call your custom initializer and pass in or some "no value specified" value for the parameter:
Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
[self initwithNibName: nibNameOrNil
bundle: nibBundleOrNil
parameter: 0];
}
With this approach, the new initWithNibName:bundle

arameter init method becomes the new "designated initializer" for your object, and all init methods should call that method.