Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2012, 10:56 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default how to pass parameter to new xib file..?

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!
vogueestylee is offline   Reply With Quote
Old 01-28-2012, 11:33 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vogueestylee View Post
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:bundlearameter init method becomes the new "designated initializer" for your object, and all init methods should call that method.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 01-28-2012 at 11:56 AM.
Duncan C is offline   Reply With Quote
Old 01-28-2012, 11:54 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

Hi Duncan, I was hoping that you give me an answer and you gave the answer to me as first -thank you!

but, I'm just more cocos2d so.. I tried

screen.month = 1;
but there is an error: Property 'month' not found..

so I need to make a one?

in .h file? like what?

NSInteger *month;

..or? I feel this is a 'little' beginner question but still.. don't know... :/
vogueestylee is offline   Reply With Quote
Old 01-28-2012, 12:11 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

thank you Duncan! great!

It is working now, just having Sematic Issue

Instance method -initWithNibName:bundlearameter not found (retur type defaults to 'id')

is it ok? apple will approve this app?
vogueestylee is offline   Reply With Quote
Old 01-28-2012, 01:20 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vogueestylee View Post
thank you Duncan! great!

It is working now, just having Sematic Issue

Instance method -initWithNibName:bundlearameter not found (retur type defaults to 'id')

is it ok? apple will approve this app?
You need to declare your new init method in your .h file.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-28-2012, 02:00 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Unhappy

in a .h file???

UIKit.h? or in AboutView.h? but where to insert it?

my file is

Code:
#import <UIKit/UIKit.h>



@interface AboutView : UIViewController
{


    IBOutlet UIButton *next;
    IBOutlet UIButton *back;
    IBOutlet UILabel *numbersLabel;
    NSArray *allLines;
    int month;
    NSString *data;

}


@property (nonatomic, retain) NSArray *allLines;
-(IBAction)next:(id)sender;
-(IBAction)back:(id)sender;
-(IBAction)menu:(id)sender;

@end
and I insert the init code after property - error, after end - error, in interface error...
vogueestylee is offline   Reply With Quote
Old 01-28-2012, 02:06 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vogueestylee View Post
in a .h file???

UIKit.h? or in AboutView.h? but where to insert it?

my file is

Code:
#import <UIKit/UIKit.h>



@interface AboutView : UIViewController
{


    IBOutlet UIButton *next;
    IBOutlet UIButton *back;
    IBOutlet UILabel *numbersLabel;
    NSArray *allLines;
    int month;
    NSString *data;

}


@property (nonatomic, retain) NSArray *allLines;
-(IBAction)next:(id)sender;
-(IBAction)back:(id)sender;
-(IBAction)menu:(id)sender;

@end
and I insert the init code after property - error, after end - error, in interface error...
The code for a method goes in the .m file, not the .h file.

When you add a method to a class, you need to include the declaration of that method in the class's header file.

So if you add a new init method to AboutView.m, you need to add the declaration of that method to AboutView.h (just the method name and parameters, not the code in the curly braces)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-28-2012, 02:23 PM   #8 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

so I got it.. THANK YOU! for all others - this is a correct code:

AboutView.h

Code:
#import <UIKit/UIKit.h>



@interface AboutView : UIViewController

{


    IBOutlet UIButton *next;
    IBOutlet UIButton *back;
    IBOutlet UILabel *numbersLabel;
    NSArray *allLines;
    int month;
    NSString *data;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parameter:(int )a;
@property (nonatomic, retain) NSArray *allLines;
-(IBAction)next:(id)sender;
-(IBAction)back:(id)sender;
-(IBAction)menu:(id)sender;

@end
vogueestylee is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
11 members and 392 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:58 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0