I am getting the following warning when I am calling other view controllers..
WindowMultivewAppDelegate might not respond to -switchView to view
This is my code in the Firstviewcontroller.m
- (IBAction)swapViews:(id)sender{
WindowMultiViewAppDelegate *delegate = (WindowMultiViewAppDelegate *)[[UIApplication sharedApplication] delegate];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[delegate switchView:self toView:secondView.view];
}
this is my code in the Firstviewcontroller.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController {
}
- (IBAction)swapViews:(id)sender;
#end
in the appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
initWithNibName:#"FirstViewController" bundle:nil];
[self.window addSubview:navigationController.view];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
if ([CLLocationManager locationServicesEnabled]) {
[locationManager startUpdatingLocation];
}
[self.window makeKeyAndVisible];
return YES;
}
in the appdelegate.h
#interface WindowMultiViewAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate , MKMapViewDelegate> {
UIWindow *window;
UINavigationController *navigationController;
CLLocationManager *locationManager;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
I have placed a UInavgationcontroller into the mainWindow.xib which leads to the rootviewcontroller which i named FirstViewController
The application quites in the secondviewcontroller when a button next is pressed which has that action inside
- (IBAction)swapNext:(id)sender
{
WindowMultiViewAppDelegate *delegate = (WindowMultiViewAppDelegate *)[[UIApplication sharedApplication] delegate];
MediaViewController *mediaView = [[MediaViewController alloc] initWithNibName:#"MediaViewController" bundle:nil];
[delegate switchView:self toView:mediaView.view];
}
What am I doing wrong??
Can anybody tell me how to connect more subviews into the UINavigationController
I want one of this subviews to be a video player - offline- and the other one to be a map which find the user"s location
I am a beginner.... thank you all when I learn I am going to help others :))
I don't see that you define the switchView: method anywhere, so it's no surprise that you get a warning about this.
Related
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.
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
EDIT: Working, see code below.
Working on an application, where right now I have the AppDelegate class, and a custom UINavigationController class. The code i have right now is pretty simple, and I feel like I've setup the XIB correctly. The build succeeds with no errors. But when the app launches, the navigationcontroller isnt displayed. I do not see the nav bar nor the table view. All I see is a blank screen. Here's the bulk of my code:
//
// FTPPhotosAppDelegate.h
// FTPPhotos
//
// Created by Aaron McLeod on 11-05-30.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "RootViewController.h"
#interface FTPPhotosAppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *navigationController;
RootViewController *rootViewController;
}
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) RootViewController *rootViewController;
#end
//
// FTPPhotosAppDelegate.m
// FTPPhotos
//
// Created by Aaron McLeod on 11-05-30.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "FTPPhotosAppDelegate.h"
#implementation FTPPhotosAppDelegate
#synthesize window=_window;
#synthesize navigationController, rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init];
rootViewController = [[RootViewController alloc] init];
[self.navigationController pushViewController:rootViewController animated:NO];
[self.window addSubview:[self.navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
#interface RootViewController : UITableViewController<UIImagePickerControllerDelegate> {
NSMutableArray *photos;
UIImagePickerController *picker;
UIBarButtonItem *addPhotoButton;
}
#property (nonatomic, retain) NSMutableArray *photos;
#property (nonatomic, retain) UIImagePickerController *picker;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *addPhotoButton;
- (void) loadPhotos;
- (IBAction) addPhoto;
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
#end
#import "RootViewController.h"
#implementation RootViewController
#synthesize photos, picker, addPhotoButton;
- (void)viewDidLoad
{
self.photos = [[NSMutableArray alloc] initWithCapacity:50];
[self loadPhotos];
[super viewDidLoad];
}
Any ideas? Here's a screenshot of the XIB,
.
Let me know if you need more info.
Instead of a root View controller, make it a UINavigationController *navigationController.
then in the applicationDidFinishLaunching, say
navigationController = [[UINavigationController alloc]init];
FirstViewController *firstView = [FirstViewController alloc]init];
[self pushViewController:firstView animated:NO];
firstView.title = #"TITLE";
[self addSubview:navigationController.view];
EDIT: some example code i have in an app of mine
AppDelegate.h
#import <UIKit/UIKit.h>
#interface DragonAge2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TitleClass *titlePage = [[TitleClass alloc]init];
navigationController = [[UINavigationController alloc]init];
titlePage.title = #"Dragon Age 2";
[navigationController pushViewController:titlePage animated:NO];
[titlePage release];
// Override point for customization after application launch.
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
You need to check some points:
Is the code in application:didFinishLaunchingWithOptions: executed when you run the application? Add a NSLog(#"app launched") in this code or set a breakpoint in it to check it. If it is not called, your FTPPhotosAppDelegate object is probably not set as the delegate of your UIApplication singleton in the XIB (this connection should be done by default if you created your app using a standard template, but it's worth checking)
Is the navigationController IBOutlet of your AppDelegate properly connected to your actual UINavigationController in the XIB? Check (using an NSLog or a breakpoint) that it is not nil.
Same for the view IBOutlet of the UINavigationController object in your XIB: check that it is properly connected and not nil.
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 wend through this tutorial and created a photo gallery for the iPhone. Now I want to add it to my TabBar project. I already heard, that Three20 doesn't support XIB, so I changed my whole tab bar setup to programmatically. I think I am not too far from a final solution.
I was able to get the photo gallery working in one tab but without functions (click on a pic --> it opens, etc). There is no navigation on top of the page that leads you to the detail image page. I faced this when I removed this from didFinishLaunchingWithOptions-method in app delegate:
// Override point for customization after application launch
TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:#"demo://album" toViewController: [AlbumController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"demo://album"]];
return YES;
I had to remove it because otherwise the whole tab bar is not shown. The photo gallery uses the whole screen. I am not sure if it is just not shown, or not loaded. I also tried:
tabbar.hidesBottomBarWhenPushed = NO;
But that did not work at all. I tried to add the TTNavigator-code to loadView(), viewDidLoad() and init() in the AlbumController itself without a result. Does anyone know where I have to put this in order to get it working?
My AlbumController.h:
#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#interface AlbumController : TTThumbsViewController {
// images
NSMutableArray *images;
// parser
NSXMLParser * rssParser;
NSMutableArray * stories;
NSMutableDictionary * item;
NSString * currentElement;
NSMutableString * currentImage;
NSMutableString * currentCaption;
}
#property (nonatomic, retain) NSMutableArray *images;
#end
And my implementation of the didFinishLaunchingWithOptions-method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set up tab bar controller
tabBarController = [[UITabBarController alloc] init];
albumController = [[AlbumController alloc] init];
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
firstViewController.delegateRef = self;
tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, albumController, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
// Override point for customization after application launch
TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:#"demo://album" toViewController: [AlbumController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"demo://album"]];
return YES;
}
Thanks guys, Cheers, dooonot
Ok guys, with help of Bryan I was able to get the photo gallery running in a tab bar application. I have seen so many people out there looking for this solution so I try to explain it as good as I can.
Seems like it is not possible to use Three20 with Interface Builder, so you have to set up your tab bar application manually. This is my Three20PhotoGalleryAppDelegate.h:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "AlbumController.h"
#import "SecondViewController.h"
#import "FirstViewController.h"
#class TabBarAppViewController;
#class AlbumController;
#class SecondViewController;
#class FirstViewController;
#interface Three20PhotoGalleryAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
AlbumController *albumController;
FirstViewController *firstViewController;
SecondViewController *secondViewController;
#private
NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_;
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UITabBarController *tabBarController;
#property (nonatomic, retain) AlbumController *albumController;
#property (nonatomic, retain) SecondViewController *secondViewController;
#property (nonatomic, retain) FirstViewController *firstViewController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
#end
Please make sure that you create a new UITabBarController as well as all your ViewControllers. Let's continue with my Three20PhotoGalleryAppDelegate.m:
#import "Three20PhotoGalleryAppDelegate.h"
#import "AlbumController.h"
#import "SecondViewController.h"
#import "FirstViewController.h"
#import <Three20/Three20.h>
#implementation Three20PhotoGalleryAppDelegate
#synthesize window;
#synthesize albumController;
#synthesize firstViewController;
#synthesize secondViewController;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set up tab bar controller manually
tabBarController = [[UITabBarController alloc] init];
albumController = [[AlbumController alloc] init];
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
/* This is the essential part of the solution. You have to add the albumController to a
new navigation controller and init it as RootViewController*/
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:albumController] autorelease];
// now add all controllers to the tabBarController
tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, navController, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)URL {
TTOpenURL([URL absoluteString]);
return YES;
}
- (void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
Please note that you don't need the TTNavigator stuff from the tutorial. Now we have to get our photogallery some how. I built it up in AlbumController like in the tutorial. This is my AlbumController.h:
#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#interface AlbumController : TTThumbsViewController {
}
#property (nonatomic, retain) NSMutableArray *images;
#end
You can find the implementation of the AlbumController in the tutorial mentioned above. Now the AlbumController.m:
#import "AlbumController.h"
#import "PhotoSource.h"
#import "Photo.h"
#implementation AlbumController
#synthesize images;
- (id)init
{
if (self = [super init])
{
// Initialization code
self.title = #"Photo Gallery";
self.hidesBottomBarWhenPushed=NO;
}
return self;
}
- (void)viewDidLoad {
[self createPhotos]; // method to set up the photos array
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:#"All in Vain"
photos:images
photos2:nil];
}
-(void)createPhotos {
// your independent implementation
}
#end
As described in the problem description above, my photo gallery always used the full screen. This is bad because you cannot use your tab bar icons anymore. For this you have to add
self.hidesBottomBarWhenPushed=NO;
to your init() method like mentioned in the AlbumController-init-method above.
Sooo, that's pretty much it. I really hope someone can re-use my solution. Thanks again to Bryan.
Cheers guys,
doonot
PS: I have created a project on github. You can download the sample app here.
Try This:
tBarController = [[UITabBarController alloc] init];
actionController = [[ActionController alloc] initWithNibName:nil bundle:nil];
// Override point for customization after application launch.
TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:#"demo://album" toViewController:tBarController];
[tBarController setViewControllers:
[NSArray arrayWithObjects:actionController,nil]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"demo://album"]];
[self.window addSubview:tBarController.view];
[self.window makeKeyAndVisible];
return YES;
You can use the TTNavigatorDemo sample to learn how to use it with Tab Bar Controllers.