Change from UIViewController to UITableViewController - iphone

This one make me go crazy. I am building an iphone app where the first view is a login view. UIViewController, when the user succesfully logs in i want to display i table view. Somehow i just have big problems doing this.
In my app delegate i load my loginViewController, and then i want from the loginViewController load my listViewController.
What is the logic behind switching to a UITableViewController from a UIViewController?

you'd better to do it in your app delegate and surely NOT add the UITableViewController.view to the UIViewController.view... just add it to the UIWindow and then dismiss the old UIViewController (removeFromSuperView it's view and then release it)
EDIT:
that's how i manage:
i add a method in my appDelegate:
-(void)switchMainView;
and from my UIViewController i just call it with this:
[[[UIApplication sharedApplication] delegate] switchMainView];
in switchMainView i just
remove my UIViewController.view from superview,
release UIViewController,
alloc the UITableViewController and init it, then
add its view to the window app:
-(void)switchMainView{
if (mainView!=nil){ // mainView is the UIViewController
[mainView.view removeFromSuperview];
[mainView release];
mainView = nil;
}
Menu *vc; // Menu is my class, subClass of a UITableViewController
vc = [[Menu alloc] init];
nc = [[UINavigationController alloc] initWithRootViewController:vc];
[window addSubview:nc.view];
[vc release];
}
and then i do the same for going back, eventually

Assuming you already have your custom UITableViewController created:
YourTableViewController *vc = [[UITableViewController alloc] initWithStyle:...];
[self presentModalViewController:vc animated:YES];
[vc release];

you can use either i do'nt think there is a major impact but definitely they might have some advantage/Disadvantage over other..
for better understanding read the below tutorial.
http://cocoawithlove.com/2009/03/recreating-uitableviewcontroller-to.html

Related

How can I hide a button in a different view controller?

I am using this code to hide a button in a different view controller, but the button does not get hidden when the button is pressed to hide the button in the other view controller.
This is the code I am using to hide the button in the other view controller:
[self dismissModalViewControllerAnimated:YES];
NSLog(#"Exited");
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Why is this not working?
Thanks!
take a BOOL variable in ViewController controller and make the property and synthesize also.
and do this.
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc.check = YES;
in the view controller viewdidload
write this
if(self.check)
[mainbutton1 set hidden:YES];
The other answers should work unless...
Judging by your code I am going to guess that you are trying to hide a button on the viewController that presented the modal view?
If this is correct then what you are doing will not work as you are creating a new instance of ViewController which is not the already existing viewController you want to use.
Although the docs say that it is fine to call [self dismissModalViewControllerAnimated:YES]; from the presented modal view I tend to set up a delegate to handle the dismissal like in Apple's utitliy app template.
The reason this isn't working is because even though you have alloc'd and init'd the ViewController properly, the actual elements of that vc ViewController (including mainbutton1) have not been loaded yet.
Hitman has the right idea (and I'm voting his idea up).
Either put in a BOOL property for setting mainButton1 to hidden when the view appears, or call your [mainButton1 setHidden: YES] right after you explicitly display the view (via animation or adding subviews or whatever).
From your question it sounds like you want to hide the button in an existing view controller, whereas in your code you are creating a new one
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Either the view controller which you observe is not the one you expect or the mainbutton1 outlet is not connected properly. You can check if the memory controller is the one you expect by logging its memory address.
NSLog(#"Hid button for view controller %p", vc);
And doing the same in the viewDidAppear callback of ViewController
NSLog(#"In viewDidAppear for view controller %p", self);
It seems you want a certain button to be hidden if something has been happening somewhere else.
You COULD, somewhat as a hack (but I don't mind that very much) control this with a variable on your AppDelegate for instance.
When the "something" is happening "somewhere else", do this:
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
appDelegate.shouldHideThatOtherButtonLater = YES;
Then, when you create your new ViewController later on you could use this value to determine if your button should be visible or not like this:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
[vc.mainbutton1 setHidden: appDelegate.shouldHideThatOtherButtonLater ];
You will in this case have to prepare your AppDelegate for this by creating and synthesizing that shouldHideThatOtherButtonLater-property.

UINavigationController - basics

I'm trying to use a UINavigationController but I'm uncertain how. Up till now (for about a year), I've been using presentModalViewController and dismissModalViewController to present/dismiss view controllers.
So, this is what I did. My main view controller (the first one that shows on launch) is called MainViewController, and it extends UIViewController.
So I made this launch function in my app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainViewController *controller = [[MainViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
And in my MainViewController's viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Title";
self.navigationController.navigationBar.tintColor = [Constants barColor];
....more code...
}
But, in my MainViewController, I'd like to present another view controller called SecondViewController, which needs a UINavigationBar with a back arrow button. So do I make SecondViewController extend UIViewController and do the same thing by setting the title and backButton in the viewDidLoad method? And how do I present it? What should I do to accomplish this?
You'll need to set a root view controller up, it's easiest starting from the apple template.
Here's where the magic happens:
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"MyNib" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
The nav controller does all the work for you (back buttons, titles, animations) - it keeps track!
My workflow is this:
Setup MutableArray in the viewDidLoad, add controllers to it, e.g:
NSMutableArray *array = [[NSMutableArray alloc] init];
MyCustomViewController *customView = [[MyCustomViewController alloc] initWithNibName:#"nib" bundle:#"nil"];
customView.title = #"Second Level";
[array addObject:customView];
self.controllers = array;
Then in your delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
UIViewController *childControllerToBe = [controllers objectAtIndex:row];
[self.navigationController pushViewController:childControllerToBe animated:YES];
}
This, along with a lot more can be learnt by reading a decent beginner book such as Beginning iPhone Development
Also, apple docs are always good :)
UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.
Please visit "How to add UINavigationController Programmatically"
You should Push it onto the navigation stack.
This Lecture by Stanford's iPhone Course will teach you a lot about Navigation Bars. (It's a quick read)
Basically at the heart of it you need this code:
[self.navigationController pushViewController:SecondView];
You can use PopViewController to go back programmatically, but the Back Button is automatically created.
Here's some source code from the Lecture. It covers exactly what you are having issues with.

adding a viewControllers view to another viewController

I am completely new to iPhone development. I have two ViewControllers
ViewControllerA
ViewControllerB
ViewControllerA is the first one and launches with the app.
I have another ViewControllerB now I want to add view of ViewControllerB as subview to ViewControllerA's view when application launches.
Try this
ViewControllerB *vcb = [[ViewControllerB alloc] init];
[self.view addSubview:vcb.view];
A belated answer. I just wrote some words about my solution for this question. It can be found here:
http://blog.nguyenthanhnhon.info/2014/04/how-to-add-viewcontrollernavigationcont.html
You need to declare the object of VC globally .. otherwise you face some issues.
#interface ViewControllerA ()
{
ViewControllerB *viewControllerBObj;
}
-(void) viewDidLoad
{
[super viewDidLoad];
viewControllerBObj = [[ViewControllerB alloc]initWithNibName:#"ViewControllerB" bundle:nil];
[self.view addSubview:viewControllerBObj.view];
}
try this
in "viewDidLoad" method of "ViewController1"
ViewController2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[self addChildViewController: vc2];
[self.view addSubview: vc2.view];
You can access the view of a view controller by using it's view property. If you have pointers to two view controllers myControllerA and myControllerB then you can add B's view to A's view by using this.
[myControllerA.view addSubview:myControllerB.view];
Add [self.view addSubView:ViewControllerB.view] in the viewDidLoad() of ViewControllerA.

How to programmatically load UIViewController

I have the following code and I want to load the UIViewController. How can I initialize and load the UIViewController.
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
CC_DIRECTOR_INIT();
NSLog(#"applicationDidFinishLaunching");
MainViewController *controller = [[MainViewController alloc] init];
}
From your delegate you can do this (assuming you have IBOutlet UIWindow *window):
[window addSubview:[controller view]];
[window makeKeyAndVisible];
Once a controller is loaded, you can push others (from the UIViewController):
controller = [[MainViewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
Here is a link to the documentation for UINavigationController.pushViewController
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
TestViewController *testController = [[TestViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:testController];
[self.window makeKeyAndVisible];
Although adding a subview will work fine, you will receive the following warning unless you set your controller as RootViewController:
Application windows are expected to have a root view controller at the
end of application launch
Are you using a nib file to set up the user interface of your view? The code you currently have does load and initialize the ViewController. But you would then need to add some user interface elements to your view, and present that view controller in your application. If you arre using a nib file for your user interface, then you want:
MainViewController *controller = [[MainViewController alloc] initWithNibName:#"nibFileName" bundle:nil];
This will associate your controller with the nib file. If you are not using a nib file, you need to programmatically add each element that you wish to display.
After your view is set up, you then need to present the view controller, by either adding it as a subview to your current view, or using a navigationController to push the new viewController. You need to be more specific about exactly what you are trying to do.
I think what you want to add is:
[[NSBundle mainBundle] loadNibNamed:#"nibWithMainViewControllerAsOwner" owner:controller options:nil];
loadNibNamed:owner:options: is the only method added to NSBundle by UIKit. See NSBundle UIKit Additions Reference. And if there's any problem with outlets not being wired up correctly then check all your outlets are key-value coding compliant (alternative answer: make sure they're correctly exposed as properties).
[viewController view]
That's how to load viewController. When the view is accessed it's lazy loaded.

How to switch one view with one UIButton on it,to the one with navigation controller using thatUIButton?

i am making an application with login view. This is just simple UIViewController with one UIbutton on it.I have another UINavigatioController having UITableView as RootViewContoller with many table objects at each row.The problem is : how to switch from loginview to tableview.Also there should be no navigationcontroller on loginview..and if it is not possible then there should be a way to hide it on loginview.
For loginview i have login.xib which i load it during application startup.but after clicking UIButton on loginview it should go to TableViewController having NavigationController.Is it possible to load TableviewController with NavigationController from seperate nib.if it is then how would i set delegates and view outlets on that nib.?..
I m restless to get its reply..its my request 2 all progrmmers to get a look over it..though it seems bit easy but isn't..i m hanging it with more than 2 weeks..plz help this poor-guy!
-(IBAction)goToView2:(id)sender
{
MyTableViewController *tableView = [[MyTableViewController alloc] initWithNibName:#"MyTableViewController" bundle:nil];
UINavigationController *mySocondView =[[UINavigationController alloc]
initWithRootViewController:tableView];
[self presentModalViewController: mySocondView animated:YES];
}
first u should set delegate to ur Navigation Controller . if you know how can set delegate . use this code to go to another view with UITableView and navigation controller :
-(IBAction)goToView2:(id)sender
{
UINavigationController *mySocondView =[[UINavigationController alloc]
initWithRootViewController:[[MyTableViewController alloc]
initWithNibName:#"MyTableViewController" bundle:nil]];
[self presentModalViewController: mySocondView animated:YES];
}
on your navigation view u can continue your project :)