I'm now writing a registration screen before my tabBar shows up. My idea is to show my registration screen through this method (no dismiss function for now):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
Register *registerView = [[Register alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:registerView animated:YES];
return YES;
}
Which is in AppDelegate.m
Unfortunately it doesn't work. Could you help me solve this please?
presentModalViewController:animated: is a method of the UIViewController class. Since the app delegate is not a view controller, you can't send it this method. To display your first view controller on screen, assign it to your app's main window's rootViewController property:
self.window.rootViewController = registerView;
Related
I am testing my app on iphone and the software version is 4.2.1. I find some problems happened when switching from a customer launchview controller to the rootview controller. The code is as below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....
launchViewController = [[[LaunchImageTransition alloc] initWithNibName:#"LaunchView_iphone" bundle:nil controller:self.viewController animation:UIModalTransitionStyleCrossDissolve] autorelease];
self.window.rootViewController = launchViewController;
....
}
In the "LaunchImageTransition.m":
//do some initial work
[self presentModalViewController:self.rootViewController animated:YES];
The rootViewController is initialed in the "MainWindow.xib".
When I test on the iphone simulator, everything is ok. But when I test on the real device, I find that the viewDidAppear method of the rootViewController is called twice while the viewDidload method is called once. This result in two of the same view。
I want to know why this only happened on real device and how to fix it?
I think that the problem is that you are using the view system in a way that it is not the way it has been thought it should be used. My advice is to change the way you present your views.
I don't know if you have to do this exactly this way, but I think that a better way to go is to let the rootViewController as it should be, like the mainViewController.
At the end of the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method you can present the launchView as modal on the mainViewController.
[mainViewController presentModalViewController: launchViewController animated:YES];
And on the viewWillAppearMethod or viewDidAppearMethod of the mainViewController you can dismiss the launchViewController.
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self dismissModalViewControllerAnimated:YES];
}
I hope it helps...
I have two views - view1 and view2.
View1 is my default viewcontroller loaded from mainwindow.xib.
Depending on some condition checking, i want to load either View1 or View2, say if user registration is not done, load sign up screen for user, else go to default view controller.
How and where do I check this condition?
Please help.
Thanks in advance.
In your app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(needToLogin) {
[self setViewController:[[[ViewController2 alloc] initWithNib:#"Login View"] autorelease]];
}
[window setRootViewController:viewController];
}
This will switch your view controller to the view2 view controller if needToLogin returns true. Otherwise, it will go to the default controller specified in mainwindow.xib
Another method (since you probably need the main view controller anyway) would be to present the login view controller if its needed.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(needToLogin) {
ViewController2 *loginVC = [[[ViewController2 alloc] initWithNib:#"LoginViewController"] autorelease];
[[self viewController] presentViewController:loginVC animated:NO];
}
[window setRootViewController:viewController];
}
Note, you will have to call [self dismissViewControllerAnimated:YES] to get rid of the login view.
Edit: Respones from OP:
I tried first one,
if(loginflag){
[self setViewController:[[[SignUpViewController alloc] initWithNibName:#"SignUpViewController" bundle:nil] autorelease]];
}
[self.window setRootViewController:self.signUpView];
Try this instead:
if(loginFlag) {
[self setViewController:[[[SignUpViewController alloc] initWithNibName:#"SignUpViewController" bundle:nil] autorelease]];
}
[[self window] setRootViewController:[self viewController]];
If you are just planning on bringing up a sign-up screen if the user registration is needed, why not stick with the default view controller, but at -applicationDidBecomeActive: present a modal view controller for the sign up view?
With the information given, you could just create a flag in the AppDelegate that can be persisted based on the user registration is complete or not. Then in the "didFinishLaunching..." method you could check this flag and load the the proper view based on this.
I'm trying to call a method in the view controller from the app delegate, but Xcode says No known class method for selector 'myMethodHere'. Here's my code:
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[..]
[MainViewController myMethodHere];
[..]
return YES;
}
MainViewController.m:
-(void) myMethodHere {
[..]
}
I would try
MainViewController * vc = [[MainViewController alloc]init];
[vc myMethodHere];
[vc release];
Make sure to import your MainViewController in your app delegate .m file
make sure you add "myMethodHere" to your MainViewController .h file
You are trying to call a class method when you want to call an instance method. If the view controller is the root view controller, then you should be able to call it thus:
UIWindow *window = [UIApplication sharedApplication].keyWindow;
MainViewController *rootViewController = window.rootViewController;
[rootViewController myMethodHere];
If it's not the root view controller then you'll have to find some other way of getting hold of the instance and then calling the method as in the last line above.
If you want to access to a view controller on a story board, you may use this block of code from the AppDelegate:
MainViewController *rootViewController = (MainViewController*)self.window.rootViewController;
[rootViewController aMethod];
Remember to add the import.
In Swift, you can write it like this
UIApplication.sharedApplication().keyWindow?.rootViewController?.yourMethodName()
Try to write
-(void) myMethodHere;
in MainViewController.h
I'm trying to display an "About Page" in my application when pressing a button, but when I press the UIButton, the NSLog(#"Button pressed") is called, but the view won't load. Here is my code:
- (IBAction)pushAbout {
NSLog(#"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:#"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
I've seen this code in an example, but it was called in a UITableViewController instead of an ordianry UIView controller.
The class files "AboutView" were created as UIViewController subclass. They were created and left untouched
A guess: Your view is not in a UINavigationController, hence
self.navigationController
is actually nil, and nothing happens.
The solution would be to place the main view in a nav controller, and adding the nav controller's view to your application's main window.
A simple way of doing this:
In your app delegate, change
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController* navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
Or similar. Note that this is just to get you started, and maybe not the nicest way of doing it. (I assumed you've just created a view-based application in xcode.)
Yes you don't have a UINavigationController set.
Add
NSLog(#"nav? %#", self.navigationController);
to your action and you'll see that it dumps (null).
However, the AboutView.xib works fine if you enter this code:
[self presentModalViewController:aboutView animated:YES];
instead of
[self.navigationController pushViewController:aboutView animated:YES];
The view will show up. In you zipped example the AboutView.xib didn't contain a label, so don't wonder if it turns out to be a white page.
You can dismiss the presented modal view by using
[self dismissModalViewControllerAnimated:YES];
in your AboutViewController.
To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.
Hey im trying to display a modal view controller as soon as my tab bar controller app opens.
There is something wrong with the code below, and im 99% sure its the code for this. what do i put for the thing im calling it on?
[self presentModalViewController:promt animated:YES];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
//Displays the password prompt modally
PasswordPromViewController *promt = [[PasswordPromViewController alloc] initWithNibName:#"PasswordPromViewController" bundle:nil];
promt.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:promt animated:YES];
[promt release];
return YES;
}
any ideas would be helful!
Cheers
I'm guessing you're adding this code in the application delegate file (eg if your app is called XXX then XXXAppDelegate.m). If this is the case you cannot use:
[self presentModalViewController:promt animated:YES];
as this method has to be called on an instance of a UIViewController. If you've set up your project in the standard way then your app delegate should have an object called window, which is a reference to the main window of the application. It's probably simplest if you add the modal view controller to that, like so:
[window presentModalViewController:promt animated:YES];