Guest User

Untitled

a guest
Sep 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. iOS - Passing variable to view controller
  2. SetTeamsViewController *vc = [[SetTeamsViewController alloc] init];
  3. vc.myLabel.text = self.teamCount;
  4. [self presentModalViewController:vc animated:YES];
  5. [vc release];
  6.  
  7. SetTeamsViewController *vc = [[SetTeamsViewController alloc] initWithTeamCount:self.teamCount];
  8.  
  9. - (id)initWithTeamCount:(int)teamCount {
  10. self = [super initWithNibName:nil bundle:nil];
  11. if (self) {
  12. // Custom initialization
  13. self.teamCountLabel.text = [NSString stringWithFormat:@"%d",teamCount];
  14. }
  15. return self;
  16. }
  17.  
  18. SetTeamsViewController *vc = [[SetTeamsViewController alloc] initWithTeamCount:teamCount];
  19.  
  20. - (id)initWithTeamCount:(int)teamCount {
  21. self = [super init];
  22. if (self) {
  23. // Custom initialization
  24. str = [NSString stringWithFormat:@"%d",teamCount];
  25. }
  26. return self;
  27. }
  28.  
  29. self.teamCountLabel.text = str;
  30.  
  31. [super viewDidLoad];
  32.  
  33. SetTeamsViewController *vc = [[SetTeamsViewController alloc] init];
  34. [self presentModalViewController:vc animated:YES];
  35. vc.myLabel.text = self.teamCount;
  36. [vc release];
  37.  
  38. #import <UIKit/UIKit.h>
  39.  
  40. @interface ViewController : UIViewController
  41. {
  42. // please make your control on XIB set these IBOutlet's
  43. //I'm not showing how to connect these with XIB
  44.  
  45. IBOutlet UILabel *lblView;
  46. IBOutlet UIButton *buttonGo;
  47. }
  48. //this is method which will push the view
  49. -(IBAction)buttonGoClickAction:(id)sender;
  50.  
  51. -(IBAction)buttonGoClickAction:(id)sender
  52. {
  53. SecondViewController *secondViewObject = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
  54. //before pushing give the text
  55. secondViewObject.string = lblView.text;
  56. [self.navigationController pushViewController:secondViewObject animated:YES];
  57. }
  58.  
  59. #import <UIKit/UIKit.h>
  60.  
  61. @interface SecondViewController : UIViewController
  62. {
  63. IBOutlet UILabel *labelView;
  64. NSString *string;
  65. }
  66. //set the string property
  67. @property(nonatomic, retain) NSString *string;
  68.  
  69. @end
  70.  
  71. #import "SecondViewController.h"
  72.  
  73. @implementation SecondViewController
  74. //synthesize string here
  75. @synthesize string;
  76.  
  77. - (void)viewDidLoad
  78. {
  79. [super viewDidLoad];
  80. // Do any additional setup after loading the view from its nib.
  81.  
  82. //Here you will get the string
  83. labelView.text = string;
  84. }
  85.  
  86. // view1.h
  87.  
  88. @interface view1 : UIViewController {
  89. NSString *passingVariable;
  90. }
  91.  
  92. @property (nonatomic, strong) NSString *passingVariable;
  93.  
  94. @end
  95.  
  96. // view1.m
  97.  
  98. @implementation view1
  99.  
  100. @synthesize passingVariable;
  101.  
  102. // the rest of the implementation
  103.  
  104. @end
  105.  
  106. // view2.m
  107.  
  108. #import "view1.h"
  109.  
  110. @implementation view2
  111.  
  112. -(IBAction)changeview
  113. {
  114. view1 *myview = [[view1 alloc] init];
  115.  
  116. myview.passingVariable = [NSString stringWithString:@"Hello Variable"];
  117.  
  118. [self.navigationController pushViewController:myview animated:YES];
  119. }
  120.  
  121. @end
Add Comment
Please, Sign In to add comment