I have a working project, one of the screen is for example ViewController 1 - tableView. i am trying to pass data and navigate to ViewController 2 after selecting a row in ViewController 1. I added this code:
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
But its not passing to ViewController 2. I think the problem is with navigatorController.
Where should I add it? to xib file of ViewController 1? or to the mainAppDelegate - I dont want to touch it because its already working with its settings...
I declared UINavigationController *navigationController in my appDelegate
What should I check?
After some changes to to code (thanks to #Vakul Saini)
appDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.View1 = [[startSearching alloc] initWithNibName:#"StartSearching" bundle:nil];
self.currentNC = [[UINavigationController alloc] initWithRootViewController:self.View1];
//this line start the startSearching at the begining and its passing to //secondViewController
self.window.rootViewController = self.currentNC;
// but with this line its start from the original start page but the transfer of views //doesn't wroks
[self.window setRootViewController: self.viewController];
appDelegate.h
#property (strong, nonatomic) startSearching *View1;
#property (strong, nonatomic) UINavigationController *currentNC;
startSearching.m
detailCon *detailCo =[[detailCon alloc] initWithNibName: #"detailView" bundle: [NSBundle mainBundle]];
[detailCon release];
UINavigation *currentNC1 = [[AppDelagate sharedInstance] currentNC];
[currentVC1 pushViewController:detailCo animated:YES];
Check you have a UINavigationController in your Appdelegate or not. You need to make your app rootViewController is UINavigationController
Add your UINavigationController in appdelegate
Appdelegate.h file
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) UINavigationController *navCon;
#end
Appdelegate.m file
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize navCon = _navCon;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.navCon = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = self.navCon;
[self.window makeKeyAndVisible];
return YES;
}
Where ViewController is your firstView of app.
Related
I am trying to change a tabbed application on xcode 4 to incorporate a navigation controller without storyboard.
The 1st tab contains a table. This is the one that needs to be navigable.
Here is FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *storeDetailsTable;
}
#property (nonatomic, retain) UITableView *storeDetailsTable;
#property (nonatomic, strong) NSDictionary *resultData;
#property (nonatomic, strong) NSMutableArray *populatedStoreArray;
#property (nonatomic, strong) NSMutableArray *images;
#end
Here's the NavController.h:
#import <UIKit/UIKit.h>
#interface NavController : UINavigationController
#property (nonatomic,retain) IBOutlet UINavigationController * navController;
#end
So, I'm using NavController as a UIViewControllerSubclass and then changed it to the above.
The AppDelegate.h:
#import <UIKit/UIKit.h>
#class NavController;
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> {
IBOutlet NavController *navController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) IBOutlet NavController *navController;
#end
And AppDelegate.m:
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "NavController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
navController = [[NavController alloc] initWithNibName:#"NavController" bundle:nil];
// UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Now when I build and run it, I see 2 tabs. But the first tab is just a blank black screen depicting the Navigation controller but there's no tableview as should be.
Is there something I have missed?
Thanks..
In your code I can't see that you're initing the FirstViewController class and making it known to the NavController. The NavController needs to init with a RootViewController, like:
NavController *myNavController = [[NavController alloc] initWithRootViewController:yourFirstViewController];
A good place to find more info is at:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
If I load the app for the first time it goes to the correct survey view, but then when i shut down the app by double clicking the home button and exiting out of it on the bottom of the iphone, then reopen the app, the app crashes instead of properly going to the returning user view controller, this view controller is just named viewController and the survey one is surveyViewController...
the following code does not work:
AppDelegate.h:
#import <UIKit/UIKit.h>
#class ViewController;
#class SurveyViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) SurveyViewController *home;
#property (strong, nonatomic) ViewController *home2;
#property (nonatomic, retain) UINavigationController *navController;
#property (nonatomic, retain) UINavigationController *navController2;
#property (strong, nonatomic) ViewController *viewController2;
#property (strong, nonatomic) SurveyViewController *viewController;
#end
AppDelegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
#import "SurveyViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize viewController2 = _viewController2;
#synthesize home, home2, navController, navController2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasFilledSurvey"] == NO) {
home = [[SurveyViewController alloc] initWithNibName:#"SurveyViewController" bundle:nil];
self.navController = [[UINavigationController alloc]initWithRootViewController:home];
self.window.rootViewController = self.navController;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasFilledSurvey"];
}
else {
home2 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.navController2 = [[UINavigationController alloc]initWithRootViewController:home2];
self.window.rootViewController = self.navController2;
}
[self.window makeKeyAndVisible];
return YES;
}
Thank you
I want to create a app with only one view(TestViewController.h TestViewController.m). (no Tabbar, no Navigation Bar) Don't know why after i launch the app, the screen is totally black. It seems that the app did not load the view successfully? Since if the view is loaded, the screen should be white. Am I right or not?
Here's AppDelegate.h
#import <UIKit/UIKit.h>
#class TestViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *window;
TestViewController *testrViewController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TestViewController *testViewController;
#end
Here's AppDelegate.m
#import "AppDelegate.h"
#import "TestViewController.h"
#implementation AppDelegate
#synthesize window = window;
#synthesize testViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
It seems to me that you're not instatiating the class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// add this line
testViewController = [[TestViewController alloc] init];
[.....]
}
Hope it helps
if you are creating it programmatically, then you should also instantiate window
UIWindow *aWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = aWindow;
[aWindow release];
then your ViewController
testViewController = [[TestViewController alloc] init];
and then make it visible
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
did you create .xib for testViewController. If not then you have to add a subview over your testViewController. and then try. I hope it will work.
UIView *testView=[[UIView alloc]initwithFrame:CGRectMake(0,0,320,480)];
[testViewController addsubview:testView];
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
A newbie Objective C question. Im trying to programmaticly change view from another view. But I only succeed in activating the tab button in the tabbarcontroller.
In my ViewDidLoad I have a condition that if thats not met, load the second view instead.
Is there any kind soul that can help out a Objective-C beginner? I have googled and search stackoverflow for the answer but with no luck.
FirstViewController.h
#interface FirstViewController : UIViewController {
some variables
}
some #properties
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
- (void)viewDidLoad
{
if(condition is met) {
[self.tabBarController setSelectedIndex:1];
}
}
In my AppDelegate.h
#import <UIKit/UIKit.h>
#interface otpAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation otpAppDelegate
#synthesize window;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];
return YES;
}
Edit!
I found a solution to my problem.
int selectedindex = 1;
self.tabBarController.selectedIndex = selectedindex;
UIViewController *tempvc = [[self.tabBarController viewControllers] objectAtIndex:selectedindex];
[self.view removeFromSuperview];
[self.view addSubview:tempvc.view];
Did the trick.
Try setting the index for the tabBarController instance of the app delegate like yourAppDelegate.tabBarController
If you are using tabBarController as IBOutlet , there is no need to allocate it in your app delegate. Please remove these code from your appDelegate method (application: didFinishLaunchingWithOptions)..
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];
I have a preference that, when set, forces my application to perform some synchronization on startup.
Can I use IB to display a different initial view based on this setting?
Is there a standard way to enable this behavior?
Assuming you have a property on your app delegate that is set during your synchronization, in the initial view controller's initWithNibNamed: method check the value synced by the app delegate and load the appropriate nib by calling [super initWithNibNamed:#"thisNibInsteadOfThatNib"];
EDIT: Show code to launch a different view depending on some condition at launch
// AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UIViewController *firstViewController;
}
#property {nonatomic, retain} UIWindow *window;
#end
// AppDelegate.m
#import AppDelegate.h
#import ViewControllerOne.h
#import ViewControllerTwo.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL shouldLoadViewOne = \\ some value from preferences
if (shouldLoadViewOne) {
firstViewController = [[ViewOneController alloc] initWithNibName:#"ViewOneController" bundle:nil];
} else {
firstViewController = [[ViewTwoController alloc] initWithNibName:#"ViewTwoController" bundle:nil];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[window addSubView:[navController view]];
[window makeKeyAndVisible];
return YES;
}
EDIT 2:
Make use of NSClassFromSting() and save the name of the firstViewController to load in the preferences.
// AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
id firstViewController;
}
#property {nonatomic, retain} UIWindow *window;
- (NSString *)firstViewControllerName;
#end
// AppDelegate.m
#import AppDelegate.h
#import ViewControllerOne.h
#import ViewControllerTwo.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *viewControllerName = [self firstViewControllerName];
firstViewController = [[NSClassFromString(viewControllerName) alloc] initWithNibName:viewControllerName bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[window addSubView:[navController view]];
[window makeKeyAndVisible];
return YES;
}
- (NSString *)firstViewControllerName
{
NSString *defaultViewController = #"ViewOneController";
NSString *savedFirstViewController = // string retrieved from preferences or other persistent store
if (!savedFirstViewController)
return defaultViewController;
return savedFirstViewController;
}