-4
IBOutlet UILabel *display;
IBOutlet UITextField *textfield;

and, I have this button to update the text from textfield to label

-(IBAction)updateLabel:(id)sender
{
    display.text=textfield.text;
}

How to also transfer such string to model ( say string "hello world" to Save.h) for further use? for example, I may want to save those strings in a file.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94

2 Answers2

1

There are multiple ways, so you should probably learn and get use to knowing your options. Some are quick but looking down the road you may need more options and will just end up re writing your code another way, so like I said knowing all your options is helpful because there isn't just one way to do things.

  1. delegation http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

  2. nsuserdefaults is saving quickly to the local files and reoopening to retrieve value..simple and quick for small things, not recommended for passing large data. http://iphonedevsdk.com/forum/iphone-sdk-tutorials/106311-tutorial-1-how-to-use-nsuserdefault.html

  3. singletons, loved and hated depending on who you talk to or how they are used or over used. http://www.galloway.me.uk/tutorials/singleton-classes/

  4. coredata which is to much for this question but great with a lot of strings, detail view controllers, its sql based and fast but has the most learning curve from all the options.

  5. passing in segues is a mention but not for your question.

  6. nsnotification can get it done, typically for a quick pass between two living/initiliazed objects objects. May not be best for your question but here is a tutorial. Send and receive messages through NSNotificationCenter in Objective-C?

  7. and as mentioned by another user creating a instance of the object gives you access to the properties IF you are the parent/creator of that instance...using this, to me, depends on the structure / hierarchy of your app or needs because if down the line your child object/ your save class wants to check something saved and send data back then you will have to think of one of the other options anyway.

Community
  • 1
  • 1
rezand
  • 576
  • 3
  • 11
0
  1. Create a Public Variable in Save.h

    Save.h

    @interface Save {
    }
    
    @property (nonatomic, retain) NSString *text;
    
    @end
    
  2. Synthesis the Variable in the .m file.

    Save.m

    @implementation Save
    
    @synthesize text;
    
    @end
    
  3. In the Current Controller, create an Object of Class Save.

  4. Set the value to the Variable

    -(IBAction)updateLabel:(id)iSender{
        display.text=textfield.text;
    
        Save *aSaveObject = [Save alloc] init];
        aSaveObject.text = textfield.text;
        [aSaveObject release];
    }
    
Roshit
  • 1,589
  • 1
  • 15
  • 37