Instanzvariable NSString soll von mehreren ViewControllern editiert werden

johannesm98

Registriert
Thread Starter
Dabei seit
28.02.2013
Beiträge
1
Reaktionspunkte
0
Das ist auf Englisch, weil ich das bereits in einem englischen Thread gepostet habe aber keine wirkliche Antwort bekommen habe, ich hoffe ihr könnt mir verzeihen :hamma:

Now, I've got a little bit (maybe a bit nooby :/) question. I want to declare a String, who can be edited by other View Controllers. I looked so long for this answer, but I didn't found a helpful answer (maybe nobody ask for it, because everybody now this). Somewhere I heard, I should do something with my AppDelegate, on some other place I heard I should declare the string outside the {} in an .h file and implent it with "extern" in a other View Controller. But I didn't find any comment, who could help me.
Here's a Test Project I created, it doesn't work, but I expected that.
ViewController.h:
Code:
[COLOR=gray][FONT=Consolas]#import <UIKit/UIKit.h>[/FONT][/COLOR]
Code:
[COLOR=gray]#import "secondViewController.h"[/COLOR][COLOR=#2B91AF]NSString[/COLOR] *theString = @[COLOR=#800000]""[/COLOR];[COLOR=#800000]@class[/COLOR] secondViewController;[COLOR=#800000]@interface[/COLOR] [COLOR=#2B91AF]ViewController[/COLOR] : [COLOR=#2B91AF]UIViewController[/COLOR]{    [COLOR=#2B91AF]IBOutlet[/COLOR] [COLOR=#2B91AF]UILabel[/COLOR] *outputLabel;    secondViewController *second;}-([COLOR=#2B91AF]IBAction[/COLOR])changeString:(id)sender;-([COLOR=#2B91AF]IBAction[/COLOR])toSecondViewController:(id)sender;[COLOR=#000000][FONT=Arial][COLOR=#800000][FONT=Consolas]@end
[/FONT][/COLOR][/FONT][/COLOR]


Here the ViewController.m:
Code:
[COLOR=gray][FONT=Consolas]#import "ViewController.h"[/FONT][/COLOR]
Code:
[COLOR=gray]#import "secondViewController.h"[/COLOR][COLOR=#800000]@interface[/COLOR] [COLOR=#2B91AF]ViewController[/COLOR] ()[COLOR=#800000]@end[/COLOR][COLOR=#800000]@implementation[/COLOR] [COLOR=#2B91AF]ViewController[/COLOR]-([COLOR=#2B91AF]IBAction[/COLOR])changeString:(id)sender{    theString = [[COLOR=#2B91AF]NSString[/COLOR] stringWithFormat:@[COLOR=#800000]"%@ changedString1"[/COLOR], theString];    [outputLabel setText:theString];}-([COLOR=#2B91AF]IBAction[/COLOR])toSecondViewController:(id)sender{    second.modalTransitionStyle = [COLOR=#2B91AF]UIModalTransitionStyleCoverVertical[/COLOR];    [[COLOR=#00008B]self[/COLOR] presentViewController:second animated:YES completion:[COLOR=#00008B]nil[/COLOR]];}- ([COLOR=#00008B]void[/COLOR])viewDidLoad{    [[COLOR=#00008B]super[/COLOR] viewDidLoad];    second = [[secondViewController alloc]initWithNibName:@[COLOR=#800000]"secondViewController"[/COLOR] bundle:[COLOR=#00008B]nil[/COLOR]];[outputLabel setText:theString];    [COLOR=gray]// Do any additional setup after loading the view, typically from a nib. [/COLOR]
[FONT=Consolas]}[/FONT]

secondViewController.h:

#import <UIKit/UIKit.h>@interface secondViewController : UIViewController{}-(IBAction)backToFirstViewController:(id)sender;@endsecondViewController.m:

#import "secondViewController.h"@interface secondViewController ()@end@implementation secondViewController-(IBAction)backToFirstViewController:(id)sender{ [self dismissViewControllerAnimated:YES completion:nil]; theString = [NSString stringWithFormat:mad:"%@ changedString1", theString];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib.}So here's a little explanation: I have a ViewController with a Label and two Buttons ("ChangeString","To secondViewController"). The Label represent the "global" string, I want to edit from many VCs. The first button should change the String to "changedString1", that's no problem because it's in the same View Controller. The second button should display the secondViewController. This VC has one Button ("backToFirstViewController"), this button should close the VC and EDIT the "global" string and present it in the Label (viewDidLoad). But you can see at my code, that I don't really know how to do this :/
It would be sooo nice, if anyone could explain me that in simple steps I have to do or post a link to a tutorial. Thank you :)
 
Zurück
Oben Unten