Creating and Removing UIViews Programmatically - objective-c

I am learning how to manipulate view displays programmatically, I manage to display a new view in my appDelegate with the following block of code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] init];
loginController = [[LoginController alloc] init];
[navController pushViewController:loginController animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES; }
I have added a button in this view that will remove this current view and supposedly programmatically display a new view, however, I only managed to remove the view and not display the new view.
my code to display the second view is the following:
HomeController *homeView = [[HomeController alloc] init];
[self.window addSubview:homeView.view];
[homeView.view release];
Please advise.. I've been searching for hours to no avail, using Switch Views Programmatically, iPhone Views, removeSuperview..
Basically I want to create a simple login flow, at app start I will display my first view (login form), after successful login I want to discard the old view and display the second view which is my home page.

You are on the right track with using the UINavigationController. In fact, you are almost there.
You already have two view controllers - one for the login page, and one for the home page. In the didFinishLaunchingWithOptions:, push both controllers onto the UINavigationController's stack: first the "home" controller, then the "login" one. Once the login controller detects that the login has been successful, call popViewControllerAnimated: or popToRootViewControllerAnimated: to get to the home page.

Related

What is the design pattern for navigating between ViewControllers?

I currently have 1 storyboard which contains 2 view controllers: ViewController and TableViewController. The ViewController is the login view, and the TableViewController is the page that displays results (results view).
Currently, I did not create a segue from the login view to the results view. Instead, on the login view, after a user presses the login button and is authenticated, I programmatically push to the results view as follows.
XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:controller animated:YES];
Indeed, the results view shows, but there is a "< Back" button at the top left, which, if pressed, goes back to the login view.
So, my question are:
How do I get rid of the login view from the view stack? (so the back button on the results view does not show)
Is this "programmatic" way of navigating between views "bad"? Meaning, should I rely on the storyboard and segues instead? Should I navigate to a new storyboard (I've seen this question asked on SO, though I haven't visited it yet)?
I'm a newcomer, so any help is appreciated.
If you don't want to use the navigation stack, you have to use presentViewController instead of pushViewController
XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
[viewController1 presentViewController:controller animated:YES];//viewcontroller1 is current view controller
Never use the code below unless you want to have the navigationController stack in viewController you are showing
/*XYZResultsTableViewController* controller = [[XYZResultsTableViewController alloc]init];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:controller animated:YES]; */
for more information on this difference between presentViewController and UINavigationController?
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

Set SplitView Controller as root controller after Login IOS

Hi I am new to IOS development and developing IOS application in which I am using split view controller. My split view controller is not at root position.I am doing login first and then open split view controller. So what I did after user login successful I am changing root view controller. Here is what I did till now:
On sign in button click I am changing root view controller as split view controller like this
- (IBAction)signinAction:(id)sender
{
NSLog(#"inside sign in .... ");
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = [[UserProfileController alloc] init];
[self dismissViewControllerAnimated:YES completion:nil];
}
User profile is my detail view controller for my split view.
#interface UserProfileController : UIViewController<UISplitViewControllerDelegate>
#end
but after click sign in button it opens black window. Don't know whats going wrong. Am I doing some thing wrong? Need Help thank you.
first thing you should change id of your UISplitViewController to Split
after just don't make any connection between your first controller and the split view controller. Then in code switch to it with this code:
- (IBAction)signinAction:(id)sender
{
UISplitViewController *split = [self.storyboard instantiateViewControllerWithIdentifier:#"Split"];
self.view.window.rootViewController = split;
}

How to display Kal's calendar in a TabBar Controller's UIVIew?

I am creating an iPad app using XCode 4 and Storyboards. I have a TabBar Controller with two UIViews. I have my iPad app and Kal in a workspace. I am trying to copy some of the Kal sample code to get the calendar to display. This is what I have copied (with minimal changes):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// initialization...
KalViewController *kal = [[KalViewController alloc] init];
kal.title = #"Saori";
// configuration
kal.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Today" style:UIBarButtonItemStyleBordered target:self action:#selector(showAndSelectToday)];
kal.delegate = self;
EventKitDataSource *dataSource = [[EventKitDataSource alloc] init];
kal.dataSource = dataSource;
// Setup the navigation stack and display it.
navController = [[UINavigationController alloc] initWithRootViewController:kal];
[window addSubview:navController.view];
[window makeKeyAndVisible];
return YES;
}
On the last 3 lines (Setup the navigation stack and display it) I am getting errors such as undeclared "navController", and "window". I don't believe the 3 lines belong in this app because I have the TabBar Controller, but I don't know what to replace them with to display the calendar.
How do I take my existing code and display the Kal calendar in the TabBar Controller's UIView?
You are right that you don't need the bottom 3 lines, but in fact, you don't need any of these lines. If you're using StoryBoards you should be able to setup your view stack entirely from there. The only thing you might need is to add the EventKit datasource to your Kal view controller, but you can do that from [KalViewController viewDidLoad:]. Your view stack should look like this:
Window -> Tab Bar Controller -> Nav Bar Controller -> Root View Controller
So by adding your Nav Bar view controller directly to the window, you're skipping the Tab bar controller entirely.

viewController management programmatically

I have a problem about viewController. I created a program What is viewController based applicaiton. There is 4 button on mainViewController. I used this code for calling mainviewController
-(void) applicationDidFinishLaunching:(UIApplication *)application{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
Then click to any button on homePage , I go to the other viewController. This code for call another viewController belong
-(IBAction)clickCalendarButton{
calendarButton.selected=YES;
[calendarButton
setImage:[UIImage imageNamed:#"afvalkalender_pressed.png"] forState:(UIControlStateHighlighted+UIControlStateSelected)];
GarbageCalendar *garbageCalendar = [[GarbageCalendar alloc] initWithNibName:#"GarbageCalendar" bundle:nil];
[self presentModalViewController:garbageCalendar animated:YES];
}
And then I want to go home page from another viewController. But I didn' go home page viewController.
Create button on detail view controller, which calls something like this:
- (IBAction)goBack {
[self dismissModalViewControllerAnimated:YES];
}
If you want to keep your current UI design, based on modal view controllers, then I think you should ensure that your other view controllers have got a button that does the dismiss of the view. Say, e.g., a "Back" or "Done" button. When you click on that button, a delegate method is called that executes: [self dismissModalViewControllerAnimated:YES];
Look also at this document for more info, section "Dismissing a Modal View Controller".
If you would like to consider alternative approaches to your UI, you could look into using a UINavigationController, which would make your life a little bit easier with navigating back from one controller to another.

How to switch page on iPhone?

HI, everyone,
I want to ask a question about the iPhone application. I am writing a program. The program will ask the user the enter some information and press enter, after that the program will process and get some information, then the program will display the tab page with 3 tabs.
However, I don't know how to switch the page after the user enter the information, I have write the following code.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview: tabBarController.view]; // add the tab page to the view controller
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView]; // add the first page to the windows in order to let the user enter info.
[controllersView release];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
If I add the tabBarController first, the controllersView will not appear, if I change the seuqence, it don't know how to make the controllersView disappear to let the tabBarController appears.
And the process is done in the MyViewController.m. Can I
1) remove the controllersView in the MyViewController.m after I complete all the process and
2) display the tabBarController.view ?
Thank you very much.
You need to use a UINavigationController as the root view-controller for your window. You can then push the first UIViewController (MyViewController) on as the top view.
(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
When its time for the tab view to appear push it onto the Navigation Controllers stack. To get back to your top view pop the ViewController off the stack
[self.navigationController popViewControllerAnimated:YES];