For some reason my nib wont load after the homeButton is pressed. However, the NSLogs are executed properly.
- (IBAction)homePressed:(id)sender {
NSLog(#"hi");
[[NSBundle mainBundle] loadNibNamed:#"rewardViewController" owner:self options:nil];
NSLog(#"hey");
}
Anyone know what's wrong with this code?
thanks!
There is nothing syntactically wrong with your code, but I suspect that you may have misunderstood what loadNibNamed is going to do for you. If you are trying to change screens by loading a new nib file then you want to explore UINavigationController.
Your code would look something more like this:
UIViewController *viewController = [[UIViewController alloc] initWithNibName:#"ViewControllerName" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
If you really did intend to use loadNibNamed like you have above you need to assign it to something to make it useful. For example if your nib contains an instance of a UIViewController you might do something like this:
UIViewController *viewController = [[NSBundle mainBundle] loadNibNamed:#"rewardViewController" owner:self options:nil];
This would create a new instance of a UIViewController with the contents of your nib file. You could then do whatever it is that you wanted to with that UIViewController.
Related
I've a nib file in which i added 2 views. Now, i want to initate(init) my second view in my code, because i show it again and again after initate. so when i try to load nib file like:
UINib *nib = [UINib nibWithNibName:#"Taxi_Login_View" bundle:nil];
NSArray *nibArray = [nib instantiateWithOwner:self options:nil];
UIView *alert = [nibArray objectAtIndex: 1];
I get second view successfully after initate. But my first view also initate that i don't want. I just want to initate second view not first. Now what i do for this.
The instantiateWithOwner: will call the initWithCoder: of all view in the nib file.
Since you are instantiate a nib file not views with in your NIB file, all object declared in the NIB will be loaded.
If you do not want this then you will need to create separate nibs for both views.
As described in the title, i would like to push a view, from a known NIB, using a button on the navigation bar. I'm working on a UINavigationController app.
I already added the right button in IB and i linked it to the method that is in the RootViewController
I have searched everywhere but I couldn't find a way to do this method...
-(IBAction)addItems:(id)sender
{
?
}
I also tried this solution, but it isn't working either...
For example:
-(IBAction)addItems:(id)sender
{
CustomController *controller = [[CustomController alloc] initWithNibName:#"CustomController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
Where
CustomController name of your custom controller class.
#"CustomController" name of your xib file
If you just need the view from nib file, then try this..
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"QPickOneView"
owner:self
options:nil];
UIView* myView = [ nibViews objectAtIndex: 0];
self.view = myView;
If you want to push a ViewController..Then go with the Nekto's Answer.
I have a one View Controller Called ThemeViewController with three nibs, all of which are a different theme.
I want to change the nib simply by hitting a button from within the app. I placed this method from within the ThemeViewController class.
- (IBAction)switchTheme {
self = [[ThemeViewController alloc] initWithNibName:#"theme_1" bundle:nil];
}
Any help or suggestions? Thanks
Use this instead:
[[NSBundle mainBundle] loadNibNamed:#"yourNibName" owner:self options:nil];
I want to avoid laying out a view programmatically.
I want to only use a NIB file to do such work.
The problem is that the only way that I know of bringing a NIB file to life is via a controller like so:
ZBarReaderViewController* reader = [[ZBarReaderViewController alloc] init];
ScannerOverlayViewController *sovc =
[[ScannerOverlayViewController alloc] initWithNibName:#"ScannerOverlayView" bundle:nil];
reader.cameraOverlayView = sovc.view;
But this approach doesn't work out so well because stepping through the debugger shows that the views is 0x0 or Nil.
Now this may be because the controller is never added-to/displayed-on the view hierarchy and I'm trying to get a view out of it in order to recycle it as an "overlay view" on another controller that allows me to specify it.
Any thoughts on how this can be accomplished without writing the View programmatically?
You can load view from XIB file:
// I assume, that there is only one root view in interface file
NSString *nibName = #"CustomView";
NSArray *loadedObjects = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil];
UIView *view = [loadedObjects lastObject];
I have these two pieces of code. The first one works perfectly:
UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 60.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:tmp];
[tmp release];
The second one is pretty much the same, but the view doesn't show up.
CommentBox *commentBox = [[CommentBox alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:commentBox];
[commentBox release]; // Why does this remove the view?
If I remove the [commentBox release] the view surprisingly appears. But I don't see a different between these two code snippets.
The init for the CommentBox looks like this:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Load the nib:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CommentBox" owner:self options:nil];
self = [nibObjects objectAtIndex:0];
}
return self;
}
After thinking about Graham's answer I came up with the following solution:
I drag a new UIView (-> lets call it sub UIView) in Interface Builder in my main UIView
I give this sub UIView the correct size (because I cannot resize the main UIView, which is always 320x460)
I drag all my other elements in this sub UIView (so that all elements are attached to my sub UIView)
I give my sub UIView a tag number (Interface Builder -> View Attributes), e.g. "300"
In the code I do now the following within my -initWithFrame:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CommentBox" owner:self options:nil];
UIView *subView = [[nibObjects objectAtIndex:0] viewWithTag:300];
[self addSubview:subView];
Hope that helps.
Update:
I just had another idea of doing it. Instead of the tag numbers you can also create a IBOutlet UIView *viewHolder in the CommentBox class and set the outlet in IB. Then in the initWithFrame: I do the following:
[[NSBundle mainBundle] loadNibNamed:#"CommentBox" owner:self options:nil];
[self addSubview:self.viewHolder];
You're doing weird things in -initWithFrame:. I'm not 100% sure that it's causing the problem you report, but I'm pretty sure it's:
wrong; and
causing a memory leak.
I don't think replacing a view object with something dearchived from a nib in its -init… methods is a good idea. Either load the nib from a controller class, or have your initialiser load the object's subviews from the nib without replacing self.