Parameter passing with initWithNibName: - iphone

In iphone Application
I need to pass some values to a new viewcontroller object while it create from a method in another viewcontroller class so I can initialize that values in (id)initWithNibName:method of new viewcontroller then I can load those values in viewdidLoad method.
what I want to know is how do I pass the values(parameters) to the constructor(initWithNibName) of a new viewcontrollor Object like constructor overloading in java
give me some code example just showing how initWithNibName called with extra parameters and how to retrieve them in the newly created object
Thanks...
Answer
this is the way I solve the problem "Observation is a object with attributes"
in ViewControllor.h I put
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(Observation *)observation;
in ViewControllor.m file I put
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil set:(Observation *)observation{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization on passed parameter observation object
draftObservation = observation;
}
return self;
}
then I call it this way in another class
ObsevationListView *obsevationListView = [[ObservationViewControllor alloc]
initWithNibName:#"ObservationViewControllor"
bundle:nil set:observer];
[self.navigationController pushViewController:obsevationListView animated:YES];
it works fine. I'm glad if anyone get help from this

You should create another initializer in your class, something like
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andParam:(id)aParam {
...
self.param = aParam;
}

Another solution is to have a property for that parameter and set it either before or after you call using the following code:
initWithNibName:bundle:

Related

InitWithNibName returns viewController but view is nil

Other xib's auto-load fine, but not this one.
When I push this viewcontroller, initWithNibName, loadView, viewDidLoad, and viewWillAppear are called fine, but the view (and all self.xxx objects in #interface) are nil in all these methods, and I am left with an empty window under the navigationbar.
SettingsMain *newVC=[[SettingsMain alloc] initWithNibName:#"SettingsMain" bundle:nil];
[self.navigationController pushViewController:newVC animated:YES];
I was wondering if I can force setting self.view to what's in the .xib, in loadView or initWithNibName, so that all the outlets etc are initalized.
The viewcontroller has the standard code,
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
The problem is that when viewDidLoad is fired, self.view is nil.

Inherit UIViewController class and XIB

I'm developing an iOS 4 application and I have developed some classes that I want to use in others projects.
One of this classes, called MapViewController, is a UIViewController subclass. This class uses a XIB file that I've created with interface builder.
I'm wondering if a use MapViewController in another project as a super class from a new class, How can I use it associated XIB?
I want to reuse MapViewController and its XIB, but I don't know if I have to do something special with the XIB file.
I don't know how to explain this. If you need more details, please tell me.
UPDATE:
I want to create a new class that inherit from MapViewController, like this:
...
#interface NewMapViewController : MapViewController {
...
And, now if I want to continue using the same XIB (the one from MapViewController), what must I do?
Since you are gonna inherit from MapViewController, your MapViewController becomes the super class. And you also have MapViewController.xib. It seems pretty straightforward to me if you use the initializer for NewMapViewController
NewMapViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"Subclass initWithNibName called");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
Initialize your NewMapViewContoller like:
//Nib of super class is MapViewController
NewMapViewController *nmapController = [[NewMapViewController alloc] initWithNibName:#"MapViewController" bundle:[NSBundle mainBundle]];

Open Data/Image To Another View

I'm trying to find out how to open an item (example: an image) from one view to another.
Basically user taps on a button that I customize into a picture. It will then bring the user to another view with that picture on the view.
Subclass the view controller and make a new init method:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil image:(UIImage*)image
self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
if (self)
{
[imageView setImage:image]
}
return self;
}
imageView is an outlet to a UIImageView in the nib
You can replace the initWithNibName with init if it suits you.
EDIT: Based on your comments you need to pass a reference to the view controller with the image.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(id)delegate
self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
if (self)
{
viewDelegate = delegate; // you can use this to reference the view with the image
}
return self;
}

NSManagedObjectContext, best way to pass it around? Access it?

I have a question regarding how to pass around NSManagedObjectContext. In my app, it seems like it's my AppDelegate that handles NSManagedObjectContext, so I shouldn't create other NSManagedObjectContexts in my other ViewControllers.
So the question is...
There is any convention or smart method to do this?
Thanks.
The way I pass the NSManagedObjectContext is to simply have an iVar in each view controller you pass it to. I usually modify the initialiser to include assignment, something like this....
MyNewViewController.h
#interface MyNewViewController : UIViewController {
NSManagedObjectContext *managedObjectContext;
}
...
MyNewViewController.m
#implementation MyNewViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andContext:(NSManagedObjectContext *)ctx {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
managedObjectContext = ctx;
}
return self;
}
....
Then when you call the view controller, you simply use the modified initialiser. Like...
MyNewViewController *nv = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil andContext:self.managedObjectContext];
Now you've got a reference to the managedObjectContext which you can use within your view controller.

How to SHOW bottombar (tab bar controller)?

I used this to hide the bottom bar
-(BOOL) hidesBottomBarWhenPushed{
return YES;
}
Now i'm at a point where I want to NOT to hide it anymore. What method should I use?
Thanks
Take at look at Elements sample project. They do something like you want, especially in the ElementViewController.m file.
Thats very simple, just use this:
[tabBar setHidesBottomBarWhenPushed:FALSE];
It took me some time putting the puzzle of John together. So here is my final result. In the .m file of my view controller I add this code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.hidesBottomBarWhenPushed = YES;
}
return self;}
Because I use a nib file I had to override the initWithNibName method.
A really simple way:
Class *instanceName = [[Class alloc] init];
instanceName.hidesBottomBarWhenPushed = YES;
...
[navigationController pushViewController:instanceName animated:YES];