开发者

How to pass a String from one .m to another .m file

开发者 https://www.devze.com 2023-02-19 22:00 出处:网络
(sorry for the bad english, im from germany) Im sorry, Im relativ new in XCode and also in objective C.

(sorry for the bad english, im from germany)

Im sorry, Im relativ new in XCode and also in objective C.

I have been searching for my problem for a long time, even here, but I didnt found the answer. I belive it is very easy, but it wont work in my projects :-(

The Problem ist, I would like to use a string in in two .m files.

For example, if I create a utility app. There will be a mainview and a flipside view. both have one .h file and one .m file.

Now I want to Fill a String in the mainview with content of a textfield, and if i switch the view, the content of the string from the main view, should be shown in a textfield on the flipside view.

I have read about propertys, synthesize... but I dont understand it. :-(

Please Please help me :-(

Shortly: I want to use one String in two .m files, so I can work with the content in both .m files, and even in both xib files.

I hope someone can help me.

regards Dennis


#import "MainViewController.h"
#import "MyFirstAnnotation.h"


@implementation MainViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)annosetzen:(id)sender{
    CLLocationCoordinate2D coor;
    coor.latitude = 54.3327162876622;
    coor.longitude = 10.1518177986145;

    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    MKCoordinateRegion region;

    region.center = coor; 
    region.span = span; 

    MyFirstAnnotation *anno = [[MyFirstAnnotation alloc] init]; 
    [mapView addAnnotation:anno];

    [mapView setRegion:region animated:TRUE];

    //MKReverseGeocoder *revGeo = [[MKReverseGeocoder alloc] initWithCoordinate:coor];
    //revGeo.delegate = self; 
    //[revGeo start];   
}

#import "MyFirstAnnotation.h"

@implementation MyFirstAnnotation
- (CLLocationCoordinate2D)coordinate { 
    CLLocationCoordinate2D coor; 
    coor.latitude = 54.3327162876622; 
    coor.longitude = 10.1518177986145; 
    return开发者_StackOverflow中文版 coor;
}
- (NSString *)title {
    return @"Name";        //This should be set from main view controller
}
- (NSString *)subtitle {
    return @"Sub";         //This also
}
@end

Now if I change the @"Name" and @"Sub" in NSStrings, how can I pass them a value from main view controller?


When your MainViewController creates the FlipSideViewController, it can give it the string in question. If your FlipSideViewController and MainViewController both have a someString property, do this in your MainViewController:

self.flipSideViewController = [[FlipSideViewController alloc] initWithNibName:nil bundle:nil];
flipSideViewController.someString = self.someString;
[self.navigationController pushViewController:flipSideViewController animated:YES];

Then, when your main view controller is about to become visible again (such as in its -viewWillAppear):

if (self.flipSideViewController) {
    self.someString = self.flipSideViewController.someString;
    self.flipSideViewController = nil;
}

This is just one way to do it, of course. Another way would be to make the main controller the delegate of the flip side controller, and have the flip side controller send it a message when someString changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消