Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 08-16-2011, 08:44 AM   #1 (permalink)
Registered Member
 
Riis's Avatar
 
Join Date: Aug 2011
Location: Denmark
Age: 23
Posts: 11
Riis is on a distinguished road
Post Passing a NSString from one view to another

Hi Guys.
Im totaly new to Iphone Development, and im for sure making alot of mistakes, so i'll take any help i can get.

My problem is that im trying to pass a NSString with some text to another view, and then use the to set the value of a Label.

Here is my coding:

// AfdelingsViewController.h

Code:
#import <UIKit/UIKit.h>

@class SecondViewController;

@interface AfdelingsViewController : UIViewController {

	IBOutlet UILabel *afdeling;
	SecondViewController *SecondController;
	
}

@property (assign) NSString *VA;
@property (nonatomic, retain) IBOutlet SecondViewController *SecondController;

-(IBAction)swapViews4:(id)sender;

@end

// AfdelingsViewController.m


#
Code:
import "AfdelingsViewController.h"
#import "FTZAppDelegate.h"
#import "SecondViewController.h"

@implementation AfdelingsViewController

@synthesize SecondController;

-(IBAction)swapViews4:(id)sender {
	
	FTZAppDelegate *delegate = (FTZAppDelegate *)[[UIApplication sharedApplication] delegate];
	SecondViewController *SecondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
	[delegate switchView4:self.view toView:SecondView.view];
	
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	/*
	if ([self.title isEqualToString:@"FTZ Skanderborg"]) {
		afdeling.text = self.SecondController.VA;
	} else {
		afdeling.text = @"FTZ Afdeling";
	}
	 */
	
	[SecondController.VA release]; 
	
	
	afdeling.text = SecondController.VA;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[super dealloc];
	[afdeling release];
}


@end
// SecondViewController.h

Code:
#import <UIKit/UIKit.h>

@class AfdelingsViewController;

@interface SecondViewController : UIViewController 
<UITableViewDelegate, UITableViewDataSource> {
	NSArray *navnJ;
	NSArray *navnF;
	NSArray *navnS;
	AfdelingsViewController *AfdelingsController;
	NSString *VA;
}

@property (nonatomic, retain) NSString *VA;
@property (nonatomic, retain) NSArray *navnJ;
@property (nonatomic, retain) NSArray *navnF;
@property (nonatomic, retain) NSArray *navnS;
@property (nonatomic, retain) IBOutlet AfdelingsViewController *AfdelingsController;

-(IBAction)swapViews:(id)sender;

@end
// SecondViewController.m

Code:
#import "SecondViewController.h"
#import "FTZAppDelegate.h"
#import "FirstViewController.h"
#import "AfdelingsViewController.h"

@implementation SecondViewController

@synthesize navnJ;
@synthesize navnF;
@synthesize navnS;
@synthesize AfdelingsController;
@synthesize VA;

-(IBAction)swapViews:(id)sender {
	
	FTZAppDelegate *delegate = (FTZAppDelegate *)[[UIApplication sharedApplication] delegate];
	FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
	[delegate switchView:self.view toView:firstView.view];
	
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.navnJ = [[NSArray alloc] initWithObjects:@"34, FTZ Skanderborg", @"35, FTZ Randers",
				  @"36, FTZ Viborg", @"37, FTZ Holstebro", @"38, FTZ Herning", nil];
	self.navnF = [[NSArray alloc] initWithObjects:@"50, FTZ Odense", @"52, FTZ Svendborg",
				  @"53, FTZ Middelfart", @"54, FTZ Faaborg", @"55, FTZ Nyborg", nil];
	self.navnS = [[NSArray alloc] initWithObjects:@"65, FTZ Næstved", @"66, FTZ Slagelse",
				  @"67, FTZ Maribo", @"68, FTZ Nykøbing Falster", @"70, FTZ Frederikssund", nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(section == 0) {
		return [self.navnJ count];
	}
	if(section == 1) {
		return [self.navnF count];
	}
	else {
		return [self.navnS count];
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	if(section == 0){
		return @"Jylland";
	}
	if(section == 1){
		return @"Fyn";
	}
	else{
		return @"Sjælland og øerne";
	}
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	if(indexPath.section == 0){
		cell.textLabel.text = [self.navnJ objectAtIndex:indexPath.row];
	}
	if(indexPath.section == 1){
		cell.textLabel.text = [self.navnF objectAtIndex:indexPath.row];
	}
	if(indexPath.section == 2){
		cell.textLabel.text = [self.navnS objectAtIndex:indexPath.row];
	}
	
    return cell;
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
	
	VA = @"FTZ Skanderborg";
	/*
	if (0 == indexPath.row)
		
	{
		VA = @"FTZ Skanderborg";
	} else {
		VA = @"Lehane";
	}
	 */
	
	FTZAppDelegate *delegate = (FTZAppDelegate *)[[UIApplication sharedApplication] delegate];
	AfdelingsViewController *AfdelingsView = [[AfdelingsViewController alloc] initWithNibName:@"AfdelingsViewController" bundle:nil];
	[delegate switchView4:self.view toView:AfdelingsView.view];
}

- (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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.navnJ = nil;
	self.navnF = nil;
	self.navnS = nil;
}

- (void)dealloc {
    [navnJ release];
	[navnF release];
	[navnS release];
    [super dealloc];
}


@end
I'm trying to pass the NSString VA from SecondViewController.m to AfdelingsViewController.m
Riis is offline   Reply With Quote
Old 08-16-2011, 11:30 AM   #2 (permalink)
***
 
huvan's Avatar
 
Join Date: May 2011
Location: Ho Chi Minh
Age: 25
Posts: 23
huvan is on a distinguished road
Default

You should create a property in AfdelingsViewController. Example: NSString *inputValue; And then:
FTZAppDelegate *delegate = (FTZAppDelegate *)[[UIApplication sharedApplication] delegate];
AfdelingsViewController *AfdelingsView = [[AfdelingsViewController alloc] initWithNibName:@"AfdelingsViewController" bundle:nil];
AfdelingsView.inputValue = VA;
[delegate switchView4:self.view toView:AfdelingsView.view];
huvan is offline   Reply With Quote
Old 08-17-2011, 01:16 AM   #3 (permalink)
Registered Member
 
Riis's Avatar
 
Join Date: Aug 2011
Location: Denmark
Age: 23
Posts: 11
Riis is on a distinguished road
Thumbs up You are the man !!

Quote:
Originally Posted by huvan View Post
You should create a property in AfdelingsViewController. Example: NSString *inputValue; And then:
FTZAppDelegate *delegate = (FTZAppDelegate *)[[UIApplication sharedApplication] delegate];
AfdelingsViewController *AfdelingsView = [[AfdelingsViewController alloc] initWithNibName:@"AfdelingsViewController" bundle:nil];
AfdelingsView.inputValue = VA;
[delegate switchView4:self.view toView:AfdelingsView.view];
Wow, very clever. Thanks alot man, it worked perfectly
Riis is offline   Reply With Quote
Old 08-18-2011, 11:27 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

what is switchView4 and switchView? I don't see anything for those anywhere.

Not really sure how this is working.
lukeirvin is offline   Reply With Quote
Old 08-18-2011, 01:06 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 210
lukeirvin is on a distinguished road
Default

This is what I am trying:

SecondViewController * secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

inputValue = [NSString stringWithFormat:@"%@", textField.text];

[secondView.label setText:inputValue];

[self presentModalViewController:secondView animated:YES];

[secondView release];
lukeirvin is offline   Reply With Quote
Old 08-22-2011, 02:07 AM   #6 (permalink)
Registered Member
 
Riis's Avatar
 
Join Date: Aug 2011
Location: Denmark
Age: 23
Posts: 11
Riis is on a distinguished road
Default

Quote:
Originally Posted by lukeirvin View Post
what is switchView4 and switchView? I don't see anything for those anywhere.

Not really sure how this is working.
switchView4 and switchView are used for switching to another view. nothing related to this problem.

- I can see that you are trying to do the same as me. "huvan's" answer solved my problem. The thing he's doing is making 2 variables (1 in each view) then he sets the value of the first variable in the first view, and then he equals this variable with the variable in the secondview. then i should work.

Let me know if you need a short exampel
Riis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
12 members and 390 guests
AppleDev, chemistry, Gi-lo, ipodphone, mistergreen2011, pipposanta, QuantumDoja, Retouchable, RobTaku, SLIC, tim0504
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,925
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:31 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0