When UIViewControllers are instantiated using - (id)initWithNibName:bundle:, you can pass in a bundle.
Is there a way to for the instantiated view controller to find out what its original bundle was?
I have a container UIViewController and I would like to instantiate its child UIViewController using the same bundle the parent was created with. Aside from saving the bundle as an ivar, is there another way to get this?
ContainerViewController *pvc = [[ContainerViewController alloc]
initWithNibName:nil bundle:[NSBundle mainBundle]];
// inside ContainerViewController:
ChildViewController *cvc = [[ChildViewController alloc]
initWithNibName:nil bundle:parentBundle];
UIViewController has a property named nibBundle which contains what you are looking for:
UIViewController class reference:
nibBundle
Return the name of the receiver’s nib bundle if it exists.
(read-only)
#property(nonatomic, readonly, retain) NSBundle *nibBundle
Availability
Available in iOS 2.0 and later.
See Also
- initWithNibName:bundle:
#property nibName
Declared In
UIViewController.h
So, you could use:
ChildViewController *cvc = [[ChildViewController alloc] initWithNibName:nil bundle:self.nibBundle];
Related
I'm using ECSliding and I have this problem!
I have a topView and two menus,
left (LeftViewController)
right (RightViewController)
both UIViewController.
I want to give a reference to the right view controller, to the left view controller, in the AppDelegate.
I did in LeftViewController.h:
#import "RightViewController.h"
#class RightViewController;
#property (strong, monatomic) RightViewController *rightView;
in didFinishLaunchingWithOptions in AppDelegate.m :
RightViewController *rightViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Right"];
LeftViewController *leftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Left"];
leftViewController.rightView = rightViewController;
but I get this error in AppDelegate.m on self.storyboard:
Property 'storyboard not found on object of type 'AppDelegate *
How can I solve this problem?
Don't statically give them a reference to each other. Instead, when they are part of the sliding view controller self.slidingViewController will be a valid reference and you can navigate based on the relationships that actually exist:
self.slidingViewController.underLeftViewController
When using it, you should check the class and cast the reference:
LeftViewController *leftController = self.slidingViewController.underLeftViewController;
if ([leftController isKindOfClass:[LeftViewController class]]) {
leftController. ...;
}
First of all AppDelegate doesn't have a property storyboard it's a UIViewController property.
Second if you want to load the main storyboard and instantiate a view controller you should try the following:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"YourStoryboardName" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"YourViewController"];
Also make sure you properly set the view controllers identifier.
I was trying to create a tab bar based application from scratch but I'm having some issues. Basically I have my AppDelegate.h, AppDelegate.m and a MainView.xib. In the the .h file I have:
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) IBOutlet UITabBarController *tabBarController;
#end
In My .m I have:
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
And in my xib file I have my files owner as a class of Appdelegate and I have my outlet from there hooked up with a Tab Bar Controller. The problem is that my controller is returning null and thus isn't being set as the rootViewController. What step/idea am I missing in setting up my app? Thanks in advance!
EDIT FIXED: Since I was starting from a empty application I didn't have 'Main nib file base name' set, so I just had to set it to the nib I was trying to load.
You need to create the view controllers that will be accessed through the tabs on your Tab Bar Controller. Then you must assign them to the viewControllers property of the Tab Bar. You can do this just before setting the Tab Bar as the window's rootViewController:
MyViewController1 *vc1 = [[MyViewController1 alloc] init];
MyViewController2 *vc2 = [[MyViewController2 alloc] init];
NSArray *controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
self.tabBarController.viewControllers = controllers;
I hope it helps!
Normally the appDelegate object is not a subclass of UIResponder, but a subclass of NSObject.
Are you hooking the TabBar instance to the first responder in your xib file? take into account that this won't work as the first responder is only a proxy object and setting the class type to it won't cause an object to be instantiated.
Instead, create an AppDelegate object in your xib file and hook the TabBar to it.
Hope it helps.
Since I was starting from a empty application I didn't have 'Main nib file base name' set, so I just had to set it to the nib I was trying to load.
I have a root view controller which should load another view controller as soon as it is done loading (i.e. in the viewDidLoad method).
I am using the UINavigationController in order to push a new view controller onto the stack:
In my rootviewcontrollerappdelegate:
-(void) viewDidLoad{
LoginViewController* lvc = [[LoginViewController alloc]init];
[self.navigationController pushViewController:lvc animated:NO];
}
I have textfields and buttons in the view controller to be loaded. The above doesn't seem to work however...It loads just a blank grey screen and no UINavigation bar is present. If I comment out the second line (pushViewController line), then I see the navigation bar. So I think it is loading something, but the items in the view controller being loaded are not being shown...Any ideas why?
Check if navigationController is pointing to nil. If it does, try
[self.view addSubview:self.pushViewController.view]
I had the same problem and found the above solution here:
UIViewController -viewDidLoad not being called
Unless you're doing something tricky, you should be calling alloc on the LoginViewController class rather than a variable. Also, if you've set up LoginViewController in Interface Builder (as opposed to programmatically), you'll need to load it from an NIB:
LoginViewController *lvc = [[[LoginViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[self.navigationController pushViewController:lvc animated:NO];
Have a look at initWithNibName:bundle: in the docs.
Not entirely sure what you are trying to achieve but when you instantiate LoginViewContoller it should probably look like this
LoginViewController* lvc = [[LoginViewController alloc]init];
Judging by the nature of your naming for your view controller, is your LoginViewController the first view controller for your UINavigationController?
If that is what you're trying to do, you should instead initialise your navigation controller with the LoginViewController as the root controller instead of pushing it onto the navigation stack.
UINavigationController has a method to do this:
- (id)initWithRootViewController:(UIViewController *)rootViewController
EDIT:
Well, one way you can go about it is like this.
In your application delegate .h file, you should have declared a UINavigationController.
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
UINavigationController *navController;
}
#property (nonatomic, retain) UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
In your App Delegate didFinishLaunching:withOption: you can create an instance of your LoginViewController there, and use that to init your UINavigation controller as the root view controller
#import "LoginViewController.h"
#implementation MyAppDelegate
#synthesize navController;
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginController = [[LoginViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:loginController];
[loginController release];
[[self window] setRootViewController:navController];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}
I probably have a typo here or there but that's one way I would go about doing it.
already i am declared navigation object like this
UINavigationController *naviButtonpro;
#property (nonatomic, retain) IBOutlet UINavigationController *naviButtonpro;
and also declared #synthesize naviButtonpro;
self.naviButtonpro = [[UINavigationController alloc] initWithNibName:#"NaviButtonViewController" bundle:nil];
[self.window addSubview:self.naviButtonpro.view];
when i am using this method
-(IBAction)displayNextPage:(id)sender
{
dispNext *gotoback = [[dispNext alloc] initWithNibName:#"dispNextView" bundle:[NSBundle mainBundle]];
//UINavigationController *naviButtonpro = [[UINavigationController alloc] initWithNibName:#"NaviButtonViewController" bundle:nil];
[self.naviButtonpro pushViewController:gotoback animated:YES];
[gotoback release];
}
i am geting an error like this
Request for member 'naviButtonpro' in something not a structure or union
can u tell me the how to resolve this problem
2 things to note here.
Firstly, you aren't creating a UINavigationController. You need to alloc init it. You seem to have it in the code but its commented out.
Secondly, dispNext needs to subclass & inherit from UIViewController. Does it do this?
The first problem is where you are initializing the 'naviButtonpro' and second is
What kind of the class its inherited of your interface class ? is it UIView or UIViewController.
I asked this question earlier with way too much code.
The ViewController initializes a UIView chain, Controller>>View>>SubView, in the ViewController. After the SubView is initialized the ViewController is set as its delegate:
aSubView.delegate = self;
NSLog(#"$#",aSubview.delegate), returns the ViewController, so I know it is set.
In the SubView, NSLog(#"$#",self.delegate),returns random crap such a hr.lproj or a file path to the Foundation framework.
It crashes when attempting to implement any of the delegates methods, since the delegate doesn't link to the ViewController but instead randomness.
This is what the SubView.h file looks like:
#import "TestDelegate.h"
#interface TestSubView : UIView {
id<TestDelegate> delegate;
}
#property (assign) id<TestDelegate> delegate;
EDIT: ViewController is initialized in the app delegate as such:
ViewController *controller = [[ViewController alloc] init];
[window addSubview:controller.view];
[controller release];
The only other thing I added to the App Delegate, over the default is an import of the ViewController header
Is it possible the view controller object is being released/dealloced between the two calls to NSLog?