How to transmit values between iPhone objective-C viewcontroller

Source: Internet
Author: User

(For passing values between viewcontrollers) the simplest and most professional method is the so-called shared instance ). The basic practice is to create a singleton class method that can instantiate this class at the first call, and then return this instance in the next call.

We use a common engine class in a board game as an example:

Engine. h

[Plain]View plaincopy

  1. # Import
  2. @ Interface Engine: nsobject {
  3. Nsuinteger Board [2, 100]; // C-style array
  4. }
  5. + (Engine *) sharedinstance;
  6. -(Nsuinteger) getfieldvalueatpos :( nsuinteger) X;
  7. -(Void) setfieldvalueatpos :( nsuinteger) x tovalue :( nsuinteger) newval;
  8. @ End

Engine. m

[Plain]View plaincopy

  1. # Import "engine. H"
  2. @ Implementation engine
  3. Static engine * _ sharedinstance;
  4. -(ID) Init
  5. {
  6. If (Self = [Super init])
  7. {
  8. // Custom Initialization
  9. Memset (board, 0, sizeof (board ));
  10. }
  11. Return self;
  12. }
  13. + (Engine *) sharedinstance
  14. {
  15. If (! _ Sharedinstance)
  16. {
  17. _ Sharedinstance = [[engine alloc] init];
  18. }
  19. Return _ sharedinstance;
  20. }
  21. -(Nsuinteger) getfieldvalueatpos :( nsuinteger) x
  22. {
  23. Return Board [x];
  24. }
  25. -(Void) setfieldvalueatpos :( nsuinteger) x tovalue :( nsuinteger) newval
  26. {
  27. Board [x] = newval;
  28. }
  29. @ End

Then we only need to import this class to our app delegate and add the following lines at the same timeCode:

[Plain]View plaincopy

  1. // At the top: # import "engine. H"
  2. Engine * myengine = [engine sharedinstance];
  3. [Myengine setfieldvalueatpos: 3 tovalue: 1];
  4. Nslog (@ "POS 3: % d", [myengine getfieldvalueatpos: 3]);
  5. Nslog (@ "POS 2: % d", [myengine getfieldvalueatpos: 2]);

You will find that you do not have the alloc-init class, but keep getting the sharedinstance pointer. In fact, this class method will create an instance during the first call and store it in static variables.

You don't have to worry about release, because the memory occupied by all apps will be cleared when they exit. But if you want to do better, you can put the sharedinstance release into the dealloc method of APP delegate. However, I found that this dealloc has never been called. I guess it is because IOS thinks that kill this app and releasing memory space will be faster.

You can also put other methods and data variables related to the engine class into the engine class. Just don't add anything about displaying it. In this way, the engine can be reused.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.