iPhone :: Objective-C - AddSubview in my initial UIViewController ViewDidAppear - iphone

Hihi all,
This could very well be a silly question. I would like to navigate to my "Login View" upon the launching of my application. My current tries:
In my first UIViewController's viewDidAppear method, perform a [self presentModalViewController:LoginView animated:YES], this works, but the screen shows my main UIView first, then slide my LoginView from bottom to top. I can't find a way to perform it without the animation.
In my first UIViewController's viewDidAppear method, perform a [self.view addSubview:LoginView.view], it ends up with exc_bad_access error.
Basically, my requirement is to perform certain checks upon starting of the application, if a login is required, the application shall display the LoginView, otherwise, it should stay as my main UIView.
Please advice what is the best way of achieving this, instead of the above two silly methods. Thanks in advance!
:)

How about trying it in **- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {**
example :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
LoginViewController *aLoginViewController = [[LoginViewController alloc] init];
[self.navigationController presentModalViewController:aLoginViewController animated:NO];
[aLoginViewController release];
return YES;
}

your 1st step is a good way..
but to stop animation, its very simple. Set animated to NO.
[self presentModalViewController:aLoginViewController animated:NO];
once ur done with ur validation, just dismiss this aLoginViewController.

Instead of -viewDidAppear, it sounds like you want to use -viewWillAppear:, which will allow you to present your login controller before the initial view is displayed.
-presentModalViewController:animated is the right method to display your login controller's view.

Related

Add a 'global' UIView to an App with StoryBoard

Is there a way to create a global UIView as background with the use of a StoryBoard?
I am updating my App, and making iOS 5 as the minimum so I can use ARC and also StoryBoards.
However, in my App I used a MainView.xib which I loaded as my rootviewcontroller, and any subsequent view was transparent so the background (and a button/copyright notice) were always visible.
I don't seem to be able to figure out how to do this. I can add the Subview to the rootview controller in the AppDelegate, but as soon as I seque to the next view it is gone.
Any suggestions how this can be done?
One way you can do this by adding an image view as a subview of the window itself, and making the backgrounds of all the view controllers clear.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIImageView *iv = [[UIImageView alloc] initWithFrame:self.window.frame];
iv.image = [UIImage imageNamed:#"BlueGreenGradient.tiff"];
[self.window addSubview:iv];
return YES;
}

Problem with getting a modalViewController to appear

I've been fighting with this for hours. I've searched around everywhere and just can't seem to find the solution to my problem. I'm pretty sure I'm just lacking some key concepts here.
My AppDelegate (didFinishLaunching) basically sets up my window and invokes RootViewController:
// create our window
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setBackgroundColor:[UIColor blackColor]];
// create our rootviewcontroller
RootViewController *controller = [[RootViewController alloc] init];
// add our rootviewcontroller's view to our window
[window addSubview:controller.view];
// controller is now owned by window's view
[controller release];
// show us to the world
[window makeKeyAndVisible];
When I add controller.view as window's subview, my understanding is that RootVC's loadView will automatically get called.
In RootVC loadView, I create a tabBarController, each tab having a navigationController and it's own viewController. All that is working fine.
In RootVC viewDidLoad, I'm checking to see if this is the first time a user is running this app, and if so, I want to throw up a modal welcome screen. This is the part I'm having trouble with.
I'd like to keep as much code out of the RootVC's viewDidLoad method, and ideally would be able to accomplish what I want with this:
WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
[self presentModalViewController:welcome animated:true];
[welcome release];
Obviously this isn't working. WelcomeVC's loadView hasn't been run yet because I haven't explicitly set it's view property. I've played around with a bunch of different solutions (welcome.view - [[UIView....], using WelcomeVC's init method to set self.view) but I just can't seem to get that modal to pop up.
How should I accomplish what I'm looking for? What are the best practices, and what's the best solution to keep my code tight and tidy?
I'm stuck, so hopefully your solution will allow me to continue developing my app!
Although the problem is not so simple, the solution is. You have to wait until the main view appears. So check the condition and present your modal view in viewDidAppear method, not in viewDidLoad method.

Why does an UIView in iOS/ appear too high up?

I'm relatively new to iOS programming. I have made a few basic apps before, and I'm getting back into it once again.
A problem I had a while back, and now is coming to haunt me is this.
When I create a new UIViewController subclass, myViewController (with xib) and add this code to get the add the view to the window, the contents always appear too high up, by the same width as the default/recommended left/right margin.
The code to add the view to the window is this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];
[window makeKeyAndVisible];
return YES;
}
For example, if I change the background colour of the view, I get a white strip at the bottom of the page when running in the simulator.
Any ideas?
Thanks
I don't think the problem is in that code. Although I guess you've done this already, it is probably a good idea to double check the .xib file. It may have an offset set in its position properties.
Also, it may be caused by the status bar not being set correctly. If you want to hide it, you can add an entry (UIStatusBarHidden -> true) in the info.plist file to set it to be hidden.
Either way check the dimensions of the .xib are the expected ones. And bear in mind the size of the status bar; the dimensions of the .xib file are different depending on whether the status bar is shown or not.

Add a new iPhone View above everything, including the navigation bar

In an application I'm developing, everything starts from a navigation controller, which then loads up several pages.
My question is, how can I load up a new view ABOVE this? The closest I've got is to do this in the App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self checkAndCreateDatabase];
[self readDataFromDatabase];
[window addSubview:[navigationController view]];
// Add this
[window addSubview:[newViewController view]];
[window makeKeyAndVisible];
return YES;
}
However this will load the view half way across the screen with no way of having it start at (0,0). Do anyone have a better suggestion?
Thanks
You can show regular views on top of everything by showing it as a modal view. Here is a sample project. http://dl.dropbox.com/u/3656129/ModalViewExample.zip

Two UITabBarControllers and Autorotation

I have two UITabBarControllers in my mainwnidow.nib wired to my appdelegate.
In my app delegate, I can load either one:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//either
[window addSubview:tabBarController.view];
//or
[window addSubview:tabBar2Controller.view];
[window makeKeyAndVisible];
}
and autorotation works.
If I add both (with the expectation to swap between them later):
[window addSubview:tabBarController.view];
[window addSubview:tabBar2Controller.view];
then autorotation stops working...everything is always portrait.
Thanks in advance for any tips...
Hmmm, I don't like this design. Typically windows should only have one subview.
Add another view controller that holds the instances of your tabBarControllers, and do the switching from there. That will fix your problem.
After thinking it through more, I realized I could just switch out the tabBarController.viewcontrollers array.
In IB I added both sets of tabs to a single tabbar, and then the appdelegate seperates them into two arrays and swaps between them as needed.
It works fine, but the jury is still out on whether the UI is effective