Load NIB from variable - iphone

I'm trying to load a NIB based on a variable I get from my settings file. This is the code:
//select the right nib name
NSString *nibVar = [nibs objectForKey:#"controller"];
// create the view controller from the selected nib name
UIViewController *aController = [[UIViewController alloc] initWithNibName:nibVar bundle:nil];
aController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:aController animated:YES];
[aController release];
This unfortunately does not work.
Any ideas here?
Thanks

You cannot instantiate "UIViewController" with arbitrary NIBs, you have to instantiate "[whatever your custom view controller class is]" with the NIB for that class.
It's crashing because it's trying to access properties that don't exist in UIViewController.
If you want to do this kind of dynamic view-controller loading, you need to do a bit more work, and use the special Class class method that lets you instantiate an object using a string for the class name, instead of hard-coded.
Sometehing like:
Class viewControllerClass = NSClassFromString( nibVar );
UIViewController* aController = (UIViewController*) [[viewControllerClass alloc] initWithNibName:nibVar bundle:nil];

Make sure the NIB name is correct and does not include the xib extension. It is also case sensitive.

Related

how to show textfields value from one view controller to another viewcontroller when button is clicked

I've implemented two uiviewcontrollers. I have some text fields in one on a uiview and 1 button on it. What I want is that when I fill text fields and click on the button, these values show on another uiview controller. I am facing a problem when I fill the text fields and click on the button, another uiview on shows on the iPhone screen but the text field values are not shown on it. I also import nextview.h file in my viewcontroller.m file. Below is some of my code:
-(IBAction)save:(id)sender
{
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil];
[[NView name] setText:name.text];
[[NView fathername] setText:father.text];
[[NView country] setText:countryselected.text];
[[NView gender] setText:genderselected.text];
[[NView dob] setText:dateselected.text];
[[NView username] setText:username.text];
[[NView password] setText:password.text];
[[NView email] setText:email.text];
[self presentModalViewController:NView animated:YES];
}
In the nextview class, I also define #property of uilabels in the .h file and I also #synthesize, initialize and release these labels in the nextview.m file and linked these variables to all labels. The problem is still there. What can I do to avoid this problem?
See this line of code:
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil];
You use a nil as nib name to create your next view. Have you overwritten initWithNibName of the nextview to give a nib name?
do you init you property your member(such as name) when call
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil]; if you init them in viewdidiload or other function about view,then they will be initialized after
you call [self presentModalViewController:NView animated:YES];
There i so many method through which you can pass data from one view to another. in this Tutorial there is brief discription about this method Which you Want Check it Tutorial Source Code
See apples documentation for initWithNibName
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
The nib is not loaded until its view is accessed. For additional initialisation use viewDidLoad.
Meaning, the outlets are just not set directly after initWithNibName.
So, one of the things you could do is add a reference to the first viewController as a property in nView and in viewDidLoad of nView you set the labels according to the first VCs labels.
On a side note: your naming convention is wrong. Use Uppercase for classes and lowercase for instances of that class ie it should be
Nextview *nView;

How do I pass object from one ViewController to another ViewController

I have a RootViewController that calls an AddQuoteViewController and there is a variable "subject_id" that I set in the RootViewController that does not show up in AddQuoteViewController and I need help understanding why and how to fix it.
Subject *sub = [self.tableDataSource objectAtIndex:indexPath.row];
self.subject_id = sub.subject_id;
[self addQuote_Clicked: sub];
...
- (void) addQuote_Clicked:(id)sender {
if(aqvController == nil)
aqvController = [[AddQuoteViewController alloc] initWithNibName:#"AddQuoteView" bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:aqvController];
[self.navigationController presentModalViewController:addNavigationController animated:YES];
}
Then in my AddQuoteViewController I try to access this variable like this:
RootViewController *rv = [RootViewController alloc] ;
NSLog(#"rv.Subject_id = %d", rv.subject_id);
But get nothing. There must be a simple way to do this.
First of all, there's an error in your third block of code. This:
RootViewController *rv = [RootViewController alloc] ;
Should be this:
RootViewController *rv = [[RootViewController alloc]init];
But strictly speaking that's not why you aren't seeing your instance variable.
If I understand correctly, the first two blocks of code are in RootViewController, and they instantiate an AddQuoteViewController and present it. Then, from your third block of code, which is in AddQuoteViewController, you want to access a member variable (subject_id) of the RootViewController that brought it up.
The approach of instantiating a RootViewController from within the AddQuoteViewController wouldn't work, because you're creating a different instance of RootViewController. What you're after is the value in the instance you just came from.
Perhaps the easiest way to do it is to create a corresponding property on AddQuoteViewController and set it when it's created:
- (void) addQuote_Clicked:(id)sender {
if(aqvController == nil)
aqvController = [[AddQuoteViewController alloc] initWithNibName:#"AddQuoteView" bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:aqvController];
aqvController.subject_id = self.subject_id;
[self.navigationController presentModalViewController:addNavigationController animated:YES];
}
You'll need to create the subject_id property on AddQuoteViewController the same way you did on RootViewController.
There are various ways of doing this, but a short answer - You can set a reference to the RootViewController as a property on your AddQuoteViewController
i.e.
in AddQuoteViewController.h
RootViewController *rvc
...
#property (nonatomic,assign) RootViewController *rvc;
and corresponding synthesize and release in your implementation class. (AddQuoteViewController.m)
Then when you create your AddQuoteViewController inside your RootViewController, also set this property:
- (void) addQuote_Clicked:(id)sender {
if(aqvController == nil)
aqvController = [[AddQuoteViewController alloc] initWithNibName:#"AddQuoteView" bundle:nil];
aqvController.rvc = self;
... etc.
Then you can access any property of the root view controller inside your AddQuoteViewController via this property:
NSLog(#"rv.Subject_id = %d", self.rv.subject_id);
As a side note there are a few things you are doing in your question that are a bit unusual, like trying to get a reference to an object by allocating a new one, and also creating a new navigation controller and presenting it as a modal view controller typically you would do one or the other (and wouldn't need to create a new navigation controller). i.e. you would either create a view controller and present it modally, or you would create a view controller and push it onto your current navigation controller stack.
You can't access variables across view controllers like that.
Have a look at creating a singleton class which will be globally accessible.
One exception I think is you can access them in the AppDelegate, but it's not advisable to have global vars.

Objective-C: Help - my xib won't show?

I have the following Objective-C code:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
NSLog(#"hello");
I have a class called 'MainMenu' with a corresponding header file and xib file. No matter what I do, it simply won't show. I have confirmed that the code gets to the above, because of the NSLog('hello').
I've been pulling my hair out for hours now and I simply cannot get to the bottom of it.
I hope someone can help,
Edit - still having problems...
Here are some screenshots of my project setup:
Ok, so I tried this:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
[self.view addSubview:main.view];
But it still doesn't work...
Many thanks in advance,
The fact that initWithNibName is nil should not be the problem because if it is given nil it looks for a nib with the exact name of the class.
Two things:
1) Make sure you have run a clean recently and make sure that file is correctly being loaded.
2) Make sure navigationController is not nil, if it is then you need to make sure you make a navigation controller if you are not intending on using a navigation controller, consider using:
- (void)presentModalViewController:(UIViewController *)modalViewController
animated:(BOOL)animated
Why are you setting the nibName to nil? If the name of the nib is MainMenu, then you want:
MainMenu *main= [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:main animated:YES];
[main release];
Are you sure that you have a UINavigationController in order to push a new view?
Hope that Helps!
Dont pull your hair just look at your code closely: You have nil in your initWithNibName. Whats MainMenu is it a viewController or what ? and place your correct nib for your to get Hello.
Updated as asked :
MainMenu *main= (MainMenu *)[MainMenu alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
[self.navigationController presentModalViewController:nav animated:YES];
NSLog(#"hello");
I guess the issue is that you do not have a navigation controller set up .Try to present the view controller by
[self presentModalViewController:main animated:YES];
Verify that the object owner is in fact MainMenu and the view is connected.
In the MainMenu NIB, select File's Owner and the click on the Identity Inspector. Class should match your VC class name.
Then select the main View in your NIB and click on the Connections Inspector. The view outlet should be connected to your File's Owner.
If those are both set correctly, then post some more surrounding code. Notable point, if those are both set,
MainMenu * main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:main animated:YES];
will load the correct NIB and display it.
What are you seeing? Another point if MainMenu is a subclass of some other VC with a NIB you will have to change the base class' init to override the default behavior, for example:
self = [super initWithNibName:nibNameOrNil == nil ? #"BaseViewController" : nibNameOrNil bundle:nibBundleOrNil]
But in that case you would have to specific the NIB that will override the base view controller's.
Post more code and let us know what you are seeing when you run.

Switch XIB to View Controller from variable

Im making a framework and I want to go back to a view controller that the publisher sets. Im using this code:
BV_APIViewControllerler *newView = [[BV_APIViewController alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
But I want to switch to a view controller they save to NSUserDefaults
What's the problem ? Juste read the name of the nib name stored in the defaults, and use it. The XIB file must reference BV_APIViewController as file's owner.

pass data to object of uiviewcontroller on iPhone

I have an iPhone app with a tableviewcontroller. When you click a certain cell it opens a new uiviewcontroller with this code:
nextViewController = [[avTouchViewController alloc] initWithNibName:#"avTouchViewController" bundle:nil];
The uiviewcontroller above called avTouchViewController has a property that looks like:
IBOutlet SomeObject *controller;
SomeObject is an object with all relevant view properties.
I would like to pass an nsstring parameter from the tableviewcontroller I initialize the avTouchViewController with to someObject.
How can I do this?
I'm a little confused by your question; you say you're creating your avTouchViewControllers when a cell is tapped inside an existing UITableView, but your last part describes the inverse situation.
Basically, if you want to pass information to a view controller, just give it a property that can be set (which may already be the case), e.g.:
nextViewController = [[avTouchViewController alloc] initWithNibName:#"avTouchViewController" bundle:nil];
nextViewController.controller = theInstanceOfSomeObjectIWantToPass;
You also may want to rename your controller property. To a reader, it doesn't make sense that a view controller has a property called controller which is actually a SomeObject*. As well, your class names should be capitalized, i.e. use AvTouchViewController instead of avTouchViewController.
If I were doing this I would add my own initializer to my UIViewController subclass:
- (id)initWithController:(NSString *pController) {
if (self = [super initWithNibName:#"avTouchViewController" bundle:nil]) {
self.controller = pController;
}
return self;
}
And then just call the following (in tableView:didSelectRowAtIndexPath: or whereever):
NSString *controller = #"Sample String";
AVTouchViewController *nextViewController = [[AVTouchViewController alloc] initWithController:controller];
[controller release];
[self.navigationController pushModalViewController:nextViewController animated:YES];
[nextViewController release];
As a point of style, class names conventionally begin with uppercase letters (hence my change to AVTouchViewController).