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