iPhone - creating a loading view - iphone

When the app is in one view controller, I want to add a view to simulate that data is being loaded when I click my tab bar controller to open another view controller.
Example: When I the app is in the recorder-view, I want it to show a loading view (a view with a activity indicator) when I change to the list of recorded files (which can take some time to load). I've tried manipulate this with the viewWillDisappear-event, but I can't get it to work - the view is not being added before after the viewDidAppear-event occurs.
Anyone have any thoughts regarding this?
Thanks
Thank you for your reply. I tried doing like tou suggested, but I still can't get it to show when I want. I try to set hidden = NO in my viewWillDisappear-event, but it does not show before that view controller disappears and the next one appears

Right now it sounds like you have a UITabBarController That takes up the whole screen. What I would do is put the loading view above the TabBarController and hide it when not necessary. I would create a subclass of loadingViewController in the same xib your tab bar controller came from (or programatically if you desire) and set it to an IBOutlet of the App Delegate.
Something like this:
//In your App Delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
loadingView.hidden = YES;
[window insertSubview:loadingViewController.view aboveSubview:abBarController.view];
[window makeKeyAndVisible];
}
//In your loading View Controller
- (void) setLoadingViewHidden:(BOOL)hidden {
self.view.hidden = hidden;
self.activityIndicator.animating = hidden;
}

The way I've done this in the past is to have a content view which houses either an activity view or the view proper.
In the view controller's nib, instead of adding subviews to the main view, leave it empty and create a new view (such as a table view in the example below) for the view proper.
Also create an activity view (with a threaded progress indicator or somesuch) and a "no results" view.
Then derive your controller class from the something like the following:
//
// ContainerViewController.h
//
#import <UIKit/UIKit.h>
#interface ContainerViewController : UIViewController
{
UIView *myContainerView;
UITableView *myTableView;
UIView *mySearchActivityView;
UIView *myZeroResultsView;
UIView *myCurrentlyShowingView;
}
#property (retain, nonatomic) IBOutlet UIView *containerView;
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#property (retain, nonatomic) IBOutlet UIView *searchActivityView;
#property (retain, nonatomic) IBOutlet UIView *zeroResultsView;
#property (assign) UIView *currentlyShowingView;
#end
//
// ContainerViewController.m
//
#import "ContainerViewController.h"
#implementation ContainerViewController
#synthesize containerView = myContainerView;
#synthesize tableView = myTableView;
#synthesize searchActivityView = mySearchActivityView;
#synthesize zeroResultsView = myZeroResultsView;
- (void)dealloc
{
[myContainerView release], myContainerView = nil;
[myTableView release], myTableView = nil;
[mySearchActivityView release], mySearchActivityView = nil;
[myZeroResultsView release], myZeroResultsView = nil;
myCurrentlyShowingView = nil;
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentlyShowingView = mySearchActivityView;
mySearchActivityView.backgroundColor = [UIColor clearColor];
myZeroResultsView.backgroundColor = [UIColor clearColor];
}
- (void)setCurrentlyShowingView:(UIView *)view
{
[myCurrentlyShowingView removeFromSuperview];
CGRect frame = view.frame;
frame.size = myContainerView.frame.size;
view.frame = frame;
[myContainerView addSubview:view];
myCurrentlyShowingView = view;
if (view == myTableView)
[myTableView reloadData];
}
- (UIView *)currentlyShowingView
{
return myCurrentlyShowingView;
}
#end
And in the -viewDidLoad method of the derived class, set off the (asynchronous) query:
- (void)viewDidLoad
{
[super viewDidLoad];
myQueryLoader = [[QueryLoader alloc] initWithQuery:#"whatever" delegate:self];
self.currentlyShowingView = mySearchActivityView;
}
and in the delegate callback:
- (void)queryLoader:(QueryLoader *)queryLoader didEndWithResults:(id)results error:(NSError *)error
{
myItems = [results retain];
if (myItems)
self.currentlyShowingView = myTableView;
else
self.currentlyShowingView = myZeroResultsView;
}
Hope this helps!

Related

UIScrollView with IUViewController dropping objects

I have a really weird issue with UIScrollView and added UIViewControllers.
It looks like that when UIViewControllers are added to UIScrollView for paging, the UIViewController drops all its added objects.
In the project i have a storyboard with two views and they are connected correctly to the corresponding code.
I know that the code doesn't move the added UIViewController to the correct X, but in this test im only adding one UIViewController so it doesnt matter.
This is the scroll code .h:
#import <UIKit/UIKit.h>
#import "TestViewController.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
#property (strong, nonatomic) NSMutableArray *scrollController;
#end
This is the scroll code .m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.scrollController = [[NSMutableArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated {
//just adding two controllers
TestViewController *first = [[TestViewController alloc] init];
[self.scrollView addSubview:first.view];
[self.scrollController addObject:first];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.scrollController.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = [self.scrollController count];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This is the viewcontroller code .h:
#import <UIKit/UIKit.h>
#interface TestViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *lblMsg;
#end
This is the viewcontroller code .m:
#import "TestViewController.h"
#interface TestViewController ()
#end
#implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"Label: %#", self.lblMsg);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The output in log is:
Label: (null)
Anybody able to see what im doing wrong?
If you create your ViewController using
TestViewController *first = [[TestViewController alloc] init];
your label (IBOutlet) won't be linked.
Navigate to your storyboard in Xcode, select your viewcontroller and assign a unique storyboard id ("myIdentifier") in the identity inspector in utilities on the right hand side.
Try
NSString *identifier = #"myIdentifier";
TestViewController *first = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
Have a look at the documentation:
Each view controller object is the sole owner of its view. You must
not associate the same view object with multiple view controller
objects. The only exception to this rule is that a container view
controller implementation may add this view as a subview in its own
view hierarchy. Before adding the subview, the container must first
call its addChildViewController: method to create a parent-child
relationship between the two view controller objects.

Change size of UIViewTable to accommodate for AdWhirl Ad

I am trying to change the size of my UITableView. I have an ad on the bottom of my view, and when I scroll, the ad scrolls along with it. I was wondering how I can change the size of the UITableView so the ad will always remain on the bottom of the view regardless of whether the UITableView is being scrolled or not. I have tried changing the size of the frame of the TableView, but this doesn't work.
- (void)viewDidAppear:(BOOL)animated
{
tableView.frame = CGRectMake()...
}
I also tried changing it in the scrollViewDidScroll: selector, but no luck. Is there anyway I can change the height so it doesn't conflict with my ad on the bottom? Thanks!
With UITableViewControllers self.view == self.tableView. This is a problem in your case because the desired effect you want requires sibling views (two views added to a common superview) but there is no "superview" for self.tableView.
You have to create a new UIViewController subclass that has a UITableView and your ad view as two subviews. You will need to handle things like setting the data source and delegate for the table view, as well as deselecting table view cells when the controller appears. This is a little more work and requires some care, but is definitely doable.
I've thrown together a quick example below that will get you started:
// Header
#interface CustomTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (id)initWithStyle:(UITableViewStyle)tableViewStyle;
#property (nonatomic, readwrite, retain) UITableView* tableView;
#end
// Source
#interface CustomTableViewController()
#property (nonatomic, readwrite, assign) UITableViewStyle tableViewStyle;
#end
#implementation CustomTableViewController
#synthesize tableView;
#synthesize tableViewStyle = _tableViewStyle;
- (id)initWithStyle:(UITableViewStyle)tableViewStyle {
if ((self = [super initWithNibName:nil bundle:nil])) {
_tableViewStyle = tableViewStyle;
}
return self;
}
- (void)loadView {
[super loadView];
self.tableView = [[UITableView alloc] initWithStyle:self.tableViewStyle];
self.tableView.autoresizingMask = (UIViewAutoresizingMaskFlexibleWidth
| UIViewAutoresizingMaskFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
// Create your ad view.
...
adView.autoresizingMask = (UIViewAutoresizingMaskFlexibleWidth
| UIViewAutoresizingMaskFlexibleTopMargin);
[self.view addSubview:adView];
[adView sizeToFit];
self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - adView.frame.size.height);
adView.frame = CGRectMake(0, self.view.bounds.size.height - adView.frame.size.height, self.view.bounds.size.width, adView.frame.size.height);
[self.tableView reloadData];
}
- (void)viewDidUnload {
self.tableView = nil;
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath* selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (nil != selectedIndexPath) {
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:animated];
}
}
#end
Simple way to solve this problem is just use .XIB file for your UITableView and then you change the height very easily using Interface Builder.
If you dont have IB file then please go through this post: How do I resize the UITableView's height dynamically?

Adding a Core Plot pie chart as a subview - the pie chart isn't getting drawn

Am a newbie to iOS programming and to Core Plot. Am using XCode 4.2 and iOS 5, and testing the iPhone App using iOS Simulator.
I know am overlooking something here (breaking my head on this for more than a day & tried google'ing a lot & tried out various possible solutions, all in vain). Can someone please help or any pointers?
Am trying to do something very simple -> adding a pie chart (done using Core Plot) as a subview. Initially, I was displaying the pie chart as a modal view & it worked fine. Now, in the same view I want to add few buttons and so I created a view (which will get displayed on pressing a button) in which I added the 2 buttons and also tried to add this pie chart too. The buttons display fine but the pie chart is not to be seen!
This is the XIB file of the view called 'GraphView' which gets displayed when a button's pressed:
In the above screenshot, the highlighted view 'View' is a UIView object within which I want to display the piechart. The entire view 'GraphView' gets displayed fine; the two buttons appear, but the piechart that I added to the subview doesn't. The UIView object that I added as subview in the above figure also gets displayed fine (I checked it by setting a background color for it). This is the code:
GRAPHVIEW.H
#import <UIKit/UIKit.h>
#import "PieChartView.h"
#interface GraphView : UIViewController
#property (strong, nonatomic) PieChartView *pieChartView;
// gView is the view which is linked to the 'UIView' subview in IB
// in the above figure
#property (strong, nonatomic) IBOutlet UIView *gView;
#property (strong, nonatomic) IBOutlet UIButton *buttonBack;
#property (strong, nonatomic) IBOutlet UIButton *buttonMail;
-(void) setHistoryData:(NSArray *)history;
...
...
#end
GRAPHVIEW.M
...
...
#synthesize gView;
#synthesize buttonBack;
#synthesize buttonMail;
...
...
-(void) setHistoryData:(NSArray *)history {
NSLog(#"GraphView: setHistoryData");
[pieChartView setHistoryData:history];
[gView addSubview:pieChartView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
pieChartView = [[PieChartView alloc] init];
return self;
}
...
...
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSLog(#"GraphView: viewDidLoad");
[super viewDidLoad];
[pieChartView initializeGraph];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
PIECHARTVIEW.H
This is the one that draws (that which is 'supposed to' draw) the pie chart!
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
// I tied subclassing 'UIViewController' or just 'UIView'...all in vain
#interface PieChartView : CPTGraphHostingView <CPTPlotDataSource> {
CPTXYGraph *graph;
CPTPieChart *pieChart;
}
#property (nonatomic, retain) CPTXYGraph *graph;
-(void) initializeGraph;
-(void) initializePieChart;
-(void) setHistoryData:(NSArray *)history;
#end
PIECHARTVIEW.m
...
#implementation PieChartView
#synthesize graph;
NSArray *historyData;
// I added the NSLog to see if these get called, but they don't seem to get called!
// So, this means that the pie chart is not being drawn actually!
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
NSLog(#"numberOfRecordsForPlot: History count: %d", (int)[historyData count]);
return [historyData count];
}
// This too isn't getting called
-(NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSLog(#"historyView: numberForPlot");
return [historyData objectAtIndex:index];
}
// This too is not getting called! OMG!
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
NSLog(#"HistoryView: dataLabelForPlot");
...
}
// This method is getting called. Am seeing the log messages
-(void) initializeGraph {
NSLog(#"HistoryView: initializeGraph");
graph = [[CPTXYGraph alloc] init];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *) self;
hostingView.hostedGraph = graph;
//hostingView.bounds = CGRectMake(5, 5, 70, 70);
[self initializePieChart];
}
// This method is also getting called. I see the log messages from this method too
/**
* Initialize the pie chart for display
*/
-(void) initializePieChart {
NSLog(#"HistoryView: initializePieChart");
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.pieRadius = 100.0;
pieChart.opaque = FALSE;
//pieChart.pieRadius = 60;
pieChart.shadowOffset = CGSizeMake(-1, 1);
pieChart.identifier = #"PieChart";
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
pieChart.labelOffset = -0.6;
[graph addPlot:pieChart];
NSLog(#"added pie chart to the graph view");
}
// This also is getting called
-(void) setHistoryData:(NSArray *)history {
NSLog(#"HistoryView: setHistoryData");
historyData = history;
}
...
...
#end
You don't need the gView property. Remove it and change the pieChartView declaration to:
#property (strong, nonatomic) IBOutlet PieChartView *pieChartView;
Link the custom view to this property instead of gView and change the class in IB to PieChartView. Also remove the initialization of this property from -initWithNibName:bundle:—the view will be initialized automatically when the nib is loaded.

Many-to-one UIViewController communication. Navigation Controller Push View. Delegate or NSNotificationCenter or Class Method?

I have a UIViewController "NavigationViewController" that is added as a subview of my "FirstViewController" that takes up the whole screen (i.e. similar to the Flipboard Navigation controller in the iPad application). Many views are added and removed based on what selection the user makes. I want to be able to push a view controller to the "FirstViewController" navigationController from within the "many views" that are added. In this case the "FeaturedViewController" is one of those many views that can be selected. It inherits ContentViewController where the delegate protocol is defined.
TL;DR I want to access the navigationcontroller in the first view to push a view from the added subview "FeaturedViewController".
Here is a visual representation:
Here is the my code from:
Here is my current attempt that does not work. Note, I took some code out relating to the "Navigation Controller"
/* First View Controller */
#import <UIKit/UIKit.h>
#import "ContentViewController.h"
#interface ViewController : UIViewController <BaseViewDelegate, ContentViewDelegate>
{
IBOutlet UINavigationController *navigationController;
ContentViewController *contentView;
}
#property (strong, nonatomic) IBOutlet UINavigationController *navigationController;
#property (strong, nonatomic) ContentViewController *contentView;
---------------------------------------------------------
#import "ViewController.h"
#import "NavigatorViewController.h"
#import "BinViewController.h"
#implementation ViewController
#synthesize navigationController, contentView;
static NSArray *viewArray = nil;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:navigationController.view];
// Navigation View is used to navigate throughout the entire application
NavigatorViewController *navController = [[NavigatorViewController alloc] init];
contentView = [[ContentViewController alloc] init];
contentView.delegate = self;
// Add the views to the array (using ARC)
viewArray = [NSArray arrayWithObjects:navController, contentView, nil];
}
-(void)displayNavigator
{
// Get the right view controller
UIViewController *viewController = [viewArray objectAtIndex:0];
// Add the subview to the view
[self.view addSubview:viewController.view];
}
-(void)pushViewController:(UIViewController *)viewController
{
NSLog(#"First View Push View Controller called");
[self.navigationController pushViewController:viewController animated:YES];
}
#end
/* Content View Controller */
#class ContentViewController;
#protocol ContentViewDelegate <NSObject>
-(void)pushViewController:(UIViewController *)viewController;
#end
#interface ContentViewController : UIViewController
{
__weak id <ContentViewDelegate> delegate;
}
#property (weak) __weak id <ContentViewDelegate> delegate;
- (void)pushView:(UIViewController *)viewController;
#end
---------------------------------------------------------
#import "ContentViewController.h"
#import "BinViewController.h"
#implementation ContentViewController
#synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)pushView:(UIViewController *)viewController
{
NSLOG(#"Push View from ContentViewController");
if ([delegate respondsToSelector:#selector(pushViewController)])
[delegate pushViewController];
}
#end
/* View that is added to the navigator view (it inherits ContentViewController where the delegate protocol is defined)*/
#import <UIKit/UIKit.h>
#import "ContentViewController.h"
#interface FeaturedViewController : ContentViewController <CustomPagingDelegate>
#end
---------------------------------------------------------
#import "FeaturedViewController.h"
#import "BinViewController.h"
#implementation FeaturedViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) touchUpInsideItemAtIndex:(NSUInteger)itemIndex
{
// [[[[[self view] superview] superview] superview] removeFromSuperview];
NSLOG(#"Touch up inside from featured view");
BinViewController *binViewController = [[BinViewController alloc] init];
[self pushView:binViewController];
}
#end
Many to one or one to many sounds like easiest route would be to use NSNotificationCenter.

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.