ios – Obj-C – 如何使用单例在viewcontrollers之间传递数据?
好吧,这是我昨晚问的问题的延伸.我对如何使用各种技术在视图控制器之间传递数据有一点了解.我想进入MVC路线,创建一个Singleton类似乎是最接近MVC的概念.
基本上我用两个View Controllers和一个singleton类创建了一个简单的应用程序.我试图将文本字段的值传递给UILabel.无论出于何种原因,它无法正常工作这就是我的代码. ViewController.h #import <UIKit/UIKit.h> #import "Model.h" #import "ViewController2.h" @interface ViewController : UIViewController { NSString *text2pass; } @property (weak,nonatomic) IBOutlet UITextField *tf; @property (weak,nonatomic) IBOutlet UILabel *btn; - (IBAction)go:(id)sender; @end ViewController.m #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize tf = _tf; @synthesize btn = _btn; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. NSString *tfstring = _tf.text; NSLog(@"string = %@",tfstring); } - (void)viewDidUnload { [self setTf:nil]; [self setBtn:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (IBAction)go:(id)sender { NSLog(@"btn pressed"); UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ViewController2 *vc2 = (ViewController2 *) [storyboard instantiateViewControllerWithIdentifier:@"home"]; text2pass = _tf.text; [self passValues]; [self presentModalViewController:vc2 animated:YES]; } -(void) passValues { Model *model = [Model sharedModel]; model.passedText = text2pass; } @end ViewController2.h #import <UIKit/UIKit.h> #import "ViewController.h" @interface ViewController2 : UIViewController { NSString *passedText; } @property (nonatomic)NSString *passedValue; @property (weak,nonatomic) IBOutlet UILabel *lbl; - (IBAction)back:(id)sender; @end ViewController2.m #import "ViewController2.h" @interface ViewController2 () { NSString *passedtext; } @end @implementation ViewController2 @synthesize lbl = _lbl; @synthesize passedValue = _passedValue; - (void)viewDidLoad { // do code stuff here NSLog(@"passedText = %@",passedText); _lbl.text = passedText; [super viewDidLoad]; } - (void)viewDidUnload { [self setLbl:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (IBAction)back:(id)sender { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ViewController *vc = (ViewController *) [storyboard instantiateViewControllerWithIdentifier:@"welcome"]; [self presentModalViewController:vc animated:YES]; } @end Model.h #import <Foundation/Foundation.h> @interface Model : NSObject { NSString *passedText; } @property (nonatomic,strong) NSString* passedText; + (Model *) sharedModel; @end Model.m #import "Model.h" @implementation Model @synthesize passedText = _passedText; static Model *sharedModel = nil; + (Model *) sharedModel { @synchronized(self){ if (sharedModel == nil){ sharedModel = [[self alloc] init]; } } return sharedModel; } @end 该项目可以从这里完整下载http://chrisrjones.com/files/KegCop-Test.zip 如果您知道为什么UILabel没有显示文本字段文本,请告诉我.哦,我几乎跟着这个 – > http://www.youtube.com/watch?v=ZFGgMPcwYjg&feature=plcp 解决方法
你的寻址和内存管理很简单……关闭.首先,绝对没有理由为此创建单例,但这不是重点.
其次,当声明属性时,(atomic,assign)默认为如果没有另外指定,这意味着你的字符串: @property (nonatomic)NSString *passedValue; 是一种薄弱的酱汁,适合在一瞬间通知的解除分配和破坏.声明它复制,强大或保留. 第三,在推送视图控制器中绝对没有引用你的单例,但你似乎相信在不同类中命名相同的对象保留它们的值(特别是在#import’ed时).不是这样.您需要引用您的单例并将[Model sharedModel] .passedText的值拉入该文本字段. 事实上,我将您的样本分为两行: //ViewController2.m #import "ViewController2.h" //actually import the singleton for access later #import "Model.h" @interface ViewController2 () { NSString *passedtext; } @end @implementation ViewController2 @synthesize lbl = _lbl; @synthesize passedValue = _passedValue; - (void)viewDidLoad { // do code stuff here NSLog(@"passedText = %@",passedText); //actually reference the singleton this time _lbl.text = [Model sharedModel].passedText; [super viewDidLoad]; } - (void)viewDidUnload { [self setLbl:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (IBAction)back:(id)sender { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ViewController *vc = (ViewController *) [storyboard instantiateViewControllerWithIdentifier:@"welcome"]; [self presentModalViewController:vc animated:YES]; } @end 这产生了这个: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |