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];
Related
I have a view, named IndicatorView, a subclass of UIView. I have a xib file for this view as well. I'd like to call this view in my ViewController using UITapGestureRecongnizer, which I have been able to do. My problem lies in how to call the view when I tap. What code should be added to my UIView file so that I can call it from the xib file. Thanks in advance.
To get an object from a xib file programatically you can use: [[NSBundle mainBundle] loadNibNamed:#"MyXibName" owner:self options:nil].
Now, in your click event of UITapGestureRecongnizer add the following code.
ObjC
IndicatorView *objIndicator = [[[NSBundle mainBundle] loadNibNamed:#"IndicatorView" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:objIndicator];
Swift 3
var objIndicator: IndicatorView? = Bundle.main.loadNibNamed("IndicatorView", owner: self, options: nil)[0]
self.view.addSubview(objIndicator)
Note: You can pass the view on which you want to add at the place of self.view
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.
I have 3 viewcontroller. FirsViewController, SecondViewController and ThirdViewController. The FirstviewController is root viewcontroller and it is adding secondviewcontrooler xib as the subview. like..
[firstviewcontrollerObject.view addsubview:secondViewcontrollerObj.view].
In secondViewcontroller I have one button. When button touch action is called, ThirdViewController is loaded. like..
[SecondViewControllerobj.view addsubview:thirdviewcontrollerobj.view];
Now ThirdViewCOntroller is on the screen. This viewcontroller has nothing only a small button. So I am seeing 2 buttons on screen. First button is of SecondviewCOntroller and second button is of ThirdViewCOntroller. But, I am able to give touch action of 3rdviewcontroller only not 2ndviewcontroller button.
How should I design my view to take touch of both button and I don't want to merge both controller i one.
I think you should implement it without using multiple UIViewControllers.
View Controllers are useful for pushing and poping from a navigation stack: with only one view displayed at time. I did not try, but I think that's why you get the bug.
But you prefered navigationControllers to the benefits of their xib, did't you?
If so, there is a way to use xib with views :
NSArray * arr1 = [[NSBundle mainBundle] loadNibNamed:#"MyView1" owner:nil options:nil];
NSArray * arr2 = [[NSBundle mainBundle] loadNibNamed:#"MyView2" owner:nil options:nil];
NSArray * arr3 = [[NSBundle mainBundle] loadNibNamed:#"MyView3" owner:nil options:nil];
UIView * v1 = [arr1 objectAtIndex:0];
UIView * v2 = [arr2 objectAtIndex:0];
UIView * v3 = [arr3 objectAtIndex:0];
// now you have your 3 views, do what you want with them:
v1.center = CGPointMake(100,100);
[self.view addSubview:v1];
v2.center = CGPointMake(100,200);
[self.view addSubview:v2];
v3.center = CGPointMake(100,300);
[self.view addSubview:v3];
that means your first view controller's contains two view which are from secontroller.view and thirdviewcontroller.view now third is taking press gesture but second is not taking press gesture.
please take two view on the firstviewcontroller.view
and do it like that
firstviewcontroller.secondVW=[[Secondviewcontroller alloc]init].view;
firstviewcontroller.thirdVW=[[Thirdviewcontroller alloc]init].view;
in this manner both the view will take touches.
In my project i have created two UIView one for landscape mode and the other for potrait mode. I am using the same same UIViewController to control both these views. These two views have the same content, only thing is that when i switch from one UIView to another the value of the UIControls is not retained.
I have loaded the UIViewenter code here using the following code
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"UIViewName"
owner:self
options:nil];
UIView *lview = (UIView *)[nibArray objectAtIndex:0];
lview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.jpg"]];
self.view = lview;
Can anybody pliz help me retaining the values during orientation change?
You can stock all the values on a NSMutableDictionary in your controller and then put the inputs of the user on the reloaded views when the user changes the orientation.
Look here at theh documentation to see how NSMutableDictionary works:
NSMutableDictionary Documentation
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];