Calling navigation controller method from background operation - iphone

I'm running a background operation using NSOperation, which gets kicked off from my AppDelegate. What I want to do is when the operation is complete, have it call a method in the RootViewController.m file.
Currently there's a property in my AppDelegate named navigationController, and I"ve been trying to set up the call like this:
AppDelegate.m:
GetRSSOperation *gro = [[GetRSSOperation alloc] initWithURL:rssURL target:self selector:#selector(dataSourceDidFinishLoadingNewData:)];
[queue addOperation:gro];
[gro release];
GetRSSOperation.m:
[target performSelectorOnMainThread:selector withObject:alerts waitUntilDone:YES];
I have tried setting the target to self, self.navigationController, self.navigationController.view but none work.
I am aware that the issue might be due to my not yet having a complete understanding of the entire ios architecture, so would appreciate any pointers please.
**Adding more code to explain:
AppDelegate.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface AppDelegate : NSObject {
NSOperationQueue *queue;
UIWindow *window;
UINavigationController *navigationController;
NSURL *rssURL;
// storage for the alert list from RSS feed
NSMutableArray *alerts;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) NSMutableArray *alerts;
#property (nonatomic, retain) NSURL *rssURL;
#property (retain) NSOperationQueue *queue;
+ (id)shared;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
#import "AlertViewController.h"
#import "RefreshTableHeader.h"
#import "GetRSSOperation.h"
#implementation AppDelegate
static AppDelegate *shared;
#synthesize window, navigationController, alerts;
- (id)init
{
if (shared) {
[self autorelease];
return shared;
}
if (![super init]) return nil;
queue = [[NSOperationQueue alloc] init];
shared = self;
return self;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(270, 3, 37, 37)];
imageview.image=[UIImage imageNamed:#"iphone.png"];
[self.navigationController.navigationBar addSubview:imageview];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
if (rssURL == nil) {
NSString *alertAddress = #"http://www.compoundstockearnings.com/com/public/selections.cfc?method=getrss&email=";
alertAddress = [alertAddress stringByAppendingString:cseLogin];
alertAddress = [alertAddress stringByAppendingString:#"&password="];
alertAddress = [alertAddress stringByAppendingString:csePass];
rssURL = [[NSURL alloc] initWithString:alertAddress];
}
GetRSSOperation *gro = [[GetRSSOperation alloc] initWithURL:rssURL target:self selector:#selector(dataSourceDidFinishLoadingNewData:)];
[queue addOperation:gro];
[gro release];
return YES;
}
return NO;
}
#end
GetRSSOperation just does the rss call, puts it in memory then tries to call the main navigation controller:
[target performSelectorOnMainThread:selector withObject:alerts waitUntilDone:YES];
The main navigation controller upon which I wish the thing to execute a selector, looks like this:
RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
IBOutlet RefreshTableHeader *alertTable;
UIImageView *imageView;
}
#end
RootViewController.m:
#import "RootViewController.h"
#import "AppDelegate.h"
#implementation RootViewController
- (void)dataSourceDidFinishLoadingNewData:(NSMutableArray *)tempAlerts
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.alerts = tempAlerts;
reloading = NO;
[alertTable flipImageAnimated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[self.tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
[alertTable setStatus:kPullToReloadStatus];
[alertTable toggleActivityView:NO];
[UIView commitAnimations];
[popSound play];
}
Obviously I've shortened it a bit but that's the core of it. I just want the GetRSSOperation to be able to call RootViewController.m - dataSourceDidFinishLoadingNewData. any input appreciated.

It sounds like you're having a bit of trouble figuring out just how to format the call to performSelectorOnMainThread:withObject:waitUntilDone: correctly. It's pretty simple, really.
If you would normally call the method like this:
[target method:alerts]
you format the performSelector call like this:
[target performSelectorOnMainThread:#selector(method:) withObject:alerts waitUntilDone:YES];

Related

UINavigationController not showing up when running build

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.

Go to another view (UIViewController) from within UIViewController

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];

TabBar Support of Three20 iPhone Photo Gallery

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.

How to pass a variable from one view controller to another?

I have three view controllers, one root controller, one login view controller and one customers view controller. I want to pass the entered username and password in login view controller to the customers view controller. My files and code is displayed below, could you please guide me, how can access to variables set in the login view controller? Or how can I pass variables to customers view controller?
I have these class files:
/classes/MySoftwareAppDelegate.h
/classes/MySoftwareAppDelegate.m
/classes/ViewController.h
/classes/ViewController.m
/classes/LoginController.h
/classes/LoginController.m
/classes/CustomersController.h
/classes/CustomersController.m
I have these views:
/resources/MainWindow.xib
/resources/Login.xib
/resources/Customers.xib
In the AppDelegate, I have successfully inserted the sub view "Login" and it's displayed whenever the app starts.
In the login view, I enter my username and password and then click the "Login" button. When this button is clicked, an IBAction is triggered. In this IBAction, I want to change the current subview with the Customers.
Here's the code I have used:
MySoftwareAppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface MySoftwareAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#end
MySoftwareAppDelegate.m
#import "MySoftwareAppDelegate.h"
#import "ViewController.h"
#implementation MySoftwareAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#class LoginController;
#interface ViewController : UIViewController {
LoginController *loginController;
}
#property (nonatomic, retain) LoginController *loginController;
#end
ViewController.m
#import "ViewController.h"
#import "LoginController.h"
#implementation ViewController
#synthesize loginController;
- (void)viewDidLoad {
LoginController *tmpViewController = [[LoginController alloc] initWithNibName:#"Login" bundle:nil];
self.loginController = tmpViewController;
[self.view insertSubview:loginController.view atIndex:0];
[tmpViewController release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
if (self.loginController.view.superview == nil) {
self.loginController = nil;
}
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[loginController release];
[super dealloc];
}
#end
LoginController.h
#import <UIKit/UIKit.h>
#class CustomersController;
#interface LoginController : UIViewController {
UIButton *loginButton;
UITextField *usernameTextField;
UITextField *passwordTextField;
NSMutableString *available_credits;
NSString *current_xml_element;
CustomersController *customersController;
}
#property (nonatomic, retain) IBOutlet UIButton *loginButton;
#property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
#property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
#property (nonatomic, retain) NSMutableString *available_credits;
#property (nonatomic, retain) NSString *current_xml_element;
#property (nonatomic, retain) CustomersController *customersController;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)loginToAccount:(id)sender;
#end
LoginController.m
#import "LoginController.h"
#import "CustomersController.h"
#implementation LoginController
#synthesize loginButton;
#synthesize usernameTextField;
#synthesize passwordTextField;
#synthesize customersController;
- (void)viewDidLoad {
UIImage *buttonImageNormal = [UIImage imageNamed:#"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[loginButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[loginButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[usernameTextField release];
[passwordTextField release];
[super dealloc];
}
-(IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
-(IBAction)backgroundTap:(id)sender {
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
-(IBAction)loginToAccount:(id)sender {
// bla bla bla... Login check process is done here
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:#"Customers" bundle:nil];
self.customersController = tmpViewController;
[self presentModalViewController:tmpViewController animated:YES];
[self.view removeFromSuperview];
[tmpViewController release];
}
#end
As you can see above, in LoginController.m's loginToAccount method, I am checking the login info and then setting the new view controller for the "customers" sub-view.
Then I am removing the current "Login" subview from the super view but don't know how to add the new "Customers" sub view.
In MainWindow.xib, I have one view controller which is linked to ViewController class and it's the root contoller.
Any help is appreciated. Because I am new to Objective-C and iPhone programming, please do your best to explain considering a novice programmer :)
Thanks again.
Okay, let me answer my question. I just found the answer on StackOverFlow.com
In the view controller which is going to load the next view controller, just add these lines:
NextController *tmpViewController = [[NextController alloc] initWithNibName:#"NextView" bundle:nil];
tmpViewController.enteredUsername = usernameTextField.text;
tmpViewController.enteredPassword = passwordTextField.text;
I'd say that better way is to have separate class for storing globally needed data (and that would be compliant with MVC model).
For example you can store you login information in your MySoftwareAppDelegate, which is easily accessible with [[UIApplication sharedApplication] delegate] call from any part of your application.
It all depends on how serious the data you want to pass it. For a quick variable (maybe a settings change in a modal view controller) TamTam's solution makes the most sense. You alloc/init'ed it, you got the variable, why not access it properties? That same (modally presented) view controller might pass variables back via a delegate pattern.
If you're data needs to be system wide, you can use the singleton pattern. Using "[[UIApplication sharedApplication] delegate]" gets the application delegation (which is a singleton), and many people stuff their variables there for convenience. However, your app delegate wasn't designed for this, and so it's considered bad form. Create your own singleton if your apple isn't a quickie.
If you use a persistent data store like sql, plists or coredata, you can put your system wide data there.

Having problem with view switching from a subview

Maybe this is a silly question but I'm having a problem with switching the view in a subview. Let me explain the code structure:
I have these class files:
/classes/MySoftwareAppDelegate.h
/classes/MySoftwareAppDelegate.m
/classes/ViewController.h
/classes/ViewController.m
/classes/LoginController.h
/classes/LoginController.m
/classes/CustomersController.h
/classes/CustomersController.m
I have these views:
/resources/MainWindow.xib
/resources/Login.xib
/resources/Customers.xib
In the AppDelegate, I have successfully inserted the sub view "Login" and it's displayed whenever the app starts.
In the login view, I enter my username and password and then click the "Login" button. When this button is clicked, an IBAction is triggered. In this IBAction, I want to change the current subview with the Customers.
Here's the code I have used:
MySoftwareAppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface MySoftwareAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#end
MySoftwareAppDelegate.m
#import "MySoftwareAppDelegate.h"
#import "ViewController.h"
#implementation MySoftwareAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#class LoginController;
#interface ViewController : UIViewController {
LoginController *loginController;
}
#property (nonatomic, retain) LoginController *loginController;
#end
ViewController.m
#import "ViewController.h"
#import "LoginController.h"
#implementation ViewController
#synthesize loginController;
- (void)viewDidLoad {
LoginController *tmpViewController = [[LoginController alloc] initWithNibName:#"Login" bundle:nil];
self.loginController = tmpViewController;
[self.view insertSubview:loginController.view atIndex:0];
[tmpViewController release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
if (self.loginController.view.superview == nil) {
self.loginController = nil;
}
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[loginController release];
[super dealloc];
}
#end
LoginController.h
#import <UIKit/UIKit.h>
#class CustomersController;
#interface LoginController : UIViewController {
UIButton *loginButton;
UITextField *usernameTextField;
UITextField *passwordTextField;
NSMutableString *available_credits;
NSString *current_xml_element;
CustomersController *customersController;
}
#property (nonatomic, retain) IBOutlet UIButton *loginButton;
#property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
#property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
#property (nonatomic, retain) NSMutableString *available_credits;
#property (nonatomic, retain) NSString *current_xml_element;
#property (nonatomic, retain) CustomersController *customersController;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)loginToAccount:(id)sender;
#end
LoginController.m
#import "LoginController.h"
#import "CustomersController.h"
#implementation LoginController
#synthesize loginButton;
#synthesize usernameTextField;
#synthesize passwordTextField;
#synthesize customersController;
- (void)viewDidLoad {
UIImage *buttonImageNormal = [UIImage imageNamed:#"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[loginButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[loginButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[usernameTextField release];
[passwordTextField release];
[super dealloc];
}
-(IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
-(IBAction)backgroundTap:(id)sender {
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
-(IBAction)loginToAccount:(id)sender {
// bla bla bla... Login check process is done here
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:#"Customers" bundle:nil];
self.customersController = tmpViewController;
[self.view removeFromSuperview];
[tmpViewController release];
}
#end
As you can see above, in LoginController.m's loginToAccount method, I am checking the login info and then setting the new view controller for the "customers" sub-view.
Then I am removing the current "Login" subview from the super view but don't know how to add the new "Customers" sub view.
In MainWindow.xib, I have one view controller which is linked to ViewController class and it's the root contoller.
Any help is appreciated. Because I am new to Objective-C and iPhone programming, please do your best to explain considering a novice programmer :)
Thanks again.
You are not adding any views to the view hierarchy, just removing the login view controller. If you want to add your customer view to the view hierarchy you should use:
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:#"Customers" bundle:nil];
self.customersController = tmpViewController;
[self presentModalViewController:tmpViewController]
The above method will make sure that the viewWillLoad, didLoad etc are called on the account view controller. It will also take care of removing and adding the correct views to the view hierarchy for you.