ViewController is loaded, but the view doesn't show up - iphone

I have a view whose controller is being instantiated (NSLog says so), but the view doesn't show up. If I load it as a modal view it appears, but not if I allocate it.
I have this structure (MenuView is the view that doesn't appear):
// ViewController.h
#import "MenuViewController.h"
#class MenuViewController;
#interface ViewController : UIViewController<ASIHTTPRequestDelegate>{
...
IBOutlet MenuViewController *menuView;
}
...
#property(nonatomic, retain) MenuViewController *menuView;
#end
// ViewController.m
#import "MenuViewController.h"
#implementation ViewController
#synthesize menuView;
- (void)loadMenu{
// THIS WORKS
// [self presentModalViewController:menuView animated:YES];
// THIS DOESN'T (VIEWCONTROLLER IS INSTANTIATED BUT VIEW DOESN'T APPEAR
menuView = [[[MenuViewController alloc] initWithNibName:#"MenuView" bundle:Nil] autorelease];
[self.navigationController pushViewController:menuView animated:YES];
}

Some ideas:
Try using self.menuView when assigning:
self.menuView = [[MenuViewController alloc] initWithNibName:#"MenuView" bundle:Nil];
Also, probably shouldn't autorelease a property. Release it in dealloc and set it to nil in viewDidUnload.
Make sure that self (ViewController) has a navigationController. Was ViewController pushed/presented by a navigationController?
Is - (void)loadMenu{ being called from the MainThread? Check with [NSThread mainThread]
Check out some tutorials/examples:
Adding a Navigation Controller by Hand
NavigationController Application in iPhone
Tutorial: Introducing UINavigationController Part 1
iPhone View Switching Tutorial

Related

How to navigate from one view to another view in single viewcontroller in iphone sdk?

May somebody let me know:
How to navigate from one view to another view in single viewcontroller in iphone sdk? I have an application in which i want to push to next view and pop to previous view in a single View controller. How this functionality can be achieved?
Thanks in advance
You can add and remove your secondview.Like this to navigate
-(IBAction)navigate:(id)sender
{
[self.view addSubView:secondView];
}
and this one to po to first view
-(IBAction)popToFirstView:(id)sender
{
[secondView removeFromSuperView];
}
Note:- You can use animations for adding and removing view, if you want to give animated effects.
If you need a single view controller you can try using a UIScrollView, that contain the 2 view, and a button to scroll from a view to another... but if i can give a tip, it's better using 2 view controller and a navigation view controller
You need to use UINavigation Controller to get push and pop.
do this way
In AppDelegate.h file
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
//this will be your first screen
#property (strong, nonatomic) FavViewController *favViewController;
#end
In A*ppDelegate.m* file
#import "AppDelegate.h"
#import "FavViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize favViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//initialize view controller
favViewController = [[FavViewController alloc] initWithNibName:#"FavViewController" bundle:nil];
//add navigation controller
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
//add controller to witndow
self.window.rootViewController = favouriteView;
[self.window makeKeyAndVisible];
return YES;
}
#end
Now come to the UIViewController, where you want to load/push to new controller
to load/push to new controller use this code
//this will be next screen
DetailsViewController *detailsViewController = [[DetailsViewController alloc ]init];
[self.navigationController pushViewController:detailsViewController animated:YES];
to go back or pop controller use this
//now it will send back to parent screen
[self.navigationController popViewControllerAnimated:YES];
I have figured out the solution for this problem and here is the solution as below:-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIButton *backbtn;
IBOutlet UIButton *nxtBtn;
IBOutlet UILabel *label;
}
-(IBAction)nxt:(id)sender;
-(IBAction)ba:(id)sender;
#end
#import "ViewController.h"
#interface ViewController ()
#end
int count=0;
#implementation ViewController
- (void)viewDidLoad
{
label.text=[[NSUserDefaults standardUserDefaults] objectForKey:#"NavigtionNumber"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)nxt:(id)sender
{
count++;
label.text=[NSString stringWithFormat:#" navigation time %d",count];
[[NSUserDefaults standardUserDefaults] setObject:label.text forKey:#"NavigtionNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
ViewController *vc=[[ViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
-(IBAction)ba:(id)sender
{
count--;
label.text=[NSString stringWithFormat:#" navigation time %d",count];
[[NSUserDefaults standardUserDefaults] setObject:label.text forKey:#"NavigtionNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
Hope this will help others in future.
Thanks.

How to communicate with different view controller?

I have a VC that presents another VC modally. This 2nd VC pushes to a 3rd VC. The 3rd VC dismisses itself and needs to call a method on the initial VC. I can't seem to get this working. Any advice?
VC3 *aVC3 = [[self.navigationController.viewControllers objectAtIndex:0] parentViewController];
aVC3 = self.name;
[aVC3 addExercise];
[self dismissView];
Use delegate for backward messsaging.
Refer simple-delegate-tutorial-for-ios-development. Also refer the-basics-of-protocols-and-delegates
If you have UINavigationController there is no need of delegate to communicate to any viewController in navigationController like:
ViewController *objViewController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0]; //need to which controller u would like to communicate
[objViewController yourMethodHere]; // your method here
Here is an alternate method:
Step 1: Create a FirstViewController Property in the SecondViewController.h
#interface SecondaryViewController : UIViewController{
}
#property (nonatomic, strong) FirstViewController *viewControllerOne;
Step 2: set that property to self when presenting the SecondViewController in FirstViewController.m
#implementation SecondaryViewController
-(void)functionThatPresentsSecondViewController{
SecondViewController *viewControllerTwo = [[SecondViewController alloc] init];
[viewControllerTwo setViewControllerOne: self];
[self presentViewController:viewControllerTwo animated:NO completion:nil];
}
Step 3: You can now call FirstViewController functions from SecondViewController by using the viewControllerTwo.viewControllerOne property.

Switching views when pressing button

I am trying to load a new view when I click a button in the app.
The error I am getting is -
View Switcher[6867:207] -[UIView pushViewController:animated:]: unrecognized selector sent to instance 0x6010660
and the source code snippet is -
-(IBAction) blueButtonPressed:(id)sender{
if(self.yellowViewController == nil){
YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:#"YellowView" bundle:nil];
self.yellowViewController = yellowController;
//[yellowController release];
//[self.view addSubview:yellowController.view];
//[self.view pushViewController:self.yellowViewController];
[self.navigationController pushViewController:self.yellowViewController];
[yellowController release];
}
//[self.navigationController pushViewController:self.yellowViewController animated:YES];
}
Here is the header file I am using -
#import <UIKit/UIKit.h>
#class BlueViewController;
#class YellowViewController;
#interface BlueViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
-(IBAction)blueButtonPressed:(id)sender;
#property(retain) YellowViewController *yellowViewController;
#property(retain, nonatomic) BlueViewController *blueViewController;
#end
The link to the xcode project is - https://rapidshare.com/files/2403429896/View_Switcher.zip
pushViewController is a UINavigationController method.
[self.navigationController pushViewController:self.yellowViewController animated:YES];
I'm assuming you are in a UIViewController subclass
Could you post an Xcode project I need to see this.
Ok, I've look into your code, the problem is that you don't have any navigationController. Your app is structured like a view base application not like a navigation base application.
The result is that your self.navigationController == nil, that is why that call is ignored.
In your application delegate you need some code looking like this
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:switchViewController];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[window makeKeyAndVisible];
In the applicationFinised...
In your code, when you click on your switch in the toolbar, it's "working" because your using this code :
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
And there is no navigationController in that process.
You should use [self.view addSubview:yellowController.view];
Best to have a navigation controller where you push and pop controllers to or from.
UIView does not have pushViewController:animated: as a method.
You can push only in UINavigationController

iPhone - pushViewController Issue

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.

UIAlertView Question

I have a small doubt. I have a NSObject class where I am trying to display an alert view. So after the alert view is displayed when I tap on OK button I want to push a navigation controller onto the stack. Can I push a navigation controller from general NSObject class? Please let me know guys..thanks for your time..
This is the code..
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
SettingsViewController *homeView = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:homeView animated:NO];
[homeView release];
}
I am creating a property called navigationController of type UINavigationController and when I catch the error I am displaying an alert view and I am using above method to push the view controller but it doesn't work..
Yes and no... depending on how you have your application set up. To push views onto the navigation stack you need to have a navigation controller.
Does your NSObject have access to this navigation controller - you might have to set up a delegate method that gets called from your delegate view when the alert view delegate gets called in your NSObject.
I'm just wondering why you're displaying a UIAlertView in an NSObject, why aren't you displaying it in a UIView or a UIViewController?
CustomObject.h
#protocol CustomObjectDelegate<NSObject>
#optional
- (void)customObjectAlertViewDidClickOk;
#end
#interface CustomObject : NSObject <UIAlertViewDelegate>{
id<CustomObjectDelegate> delegate;
}
#property (nonatomic, assign) id<CustomObjectDelegate> delegate;
#end;
CustomObject.m
#synthesize delegate;
// then put this:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
[delegate customObjectAlertViewDidClickOk];
}
Then your ViewController .h file needs to include the custom object and assign the delegate methods:
#include "CustomObject.h"
#interface MyViewController : UIViewController <CustomObjectDelegate> {
}
#end
and the .m viewDidLoad (or similar):
- (void)viewDidLoad{
CustomObject *obj = [[CustomObject alloc] init];
[obj setDelegate:self];
}
- (void)customObjectAlertViewDidClickOk{
AnotherViewController *page = [[AnotherViewController alloc] initWithNibName:nil bundles:nil];
[self.navigationController pushViewController:page];
}
Thats how I would do it - given I'm not too sure i understand quite what you're asking. :) thats all off the top of my head as well - so don't take it letter for letter, but you have the basis there to start off with. You can build on it. Look up #protocols and delegate methods, its all in there. :)