Obj-C - 如何使用单例在viewcontrollers之间传递数据? [英] Obj-C - How to pass data between viewcontrollers using a singleton?

查看:77
本文介绍了Obj-C - 如何使用单例在viewcontrollers之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这是我昨晚问的问题的延伸。我对如何使用各种技术在视图控制器之间传递数据有一点了解。我想进入MVC路线,创建一个Singleton类似乎是最接近MVC的概念。

Alright, so this is an extension to a question I asked last night. I have a little firmer grasp on how data can be passed between view controllers using various techniques. I wanted to go the MVC route, and creating a Singleton class seems the closest concept similar to MVC.

基本上我创建了一个带有两个View Controller和一个单例类的简单应用程序。我试图将文本字段的值传递给UILabel。无论出于何种原因,它无法正常工作这就是我的代码。

Basically I created a simple app with two View Controllers and a singleton class. I am trying to pass the value of a text field into a UILabel. For whatever reason it isn't working. This is what my code looks like.

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

The project can be downloaded in its entirety from here http://chrisrjones.com/files/KegCop-Test.zip

如果您知道为什么UILabel没有显示文本字段文本,请告诉我。哦,我几乎跟着这个 - > http://www.youtube.com/watch ?v = ZFGgMPcwYjg& feature = plcp

If you know why the UILabel is not displaying the text field text let me know. Oh I pretty much followed this -> http://www.youtube.com/watch?v=ZFGgMPcwYjg&feature=plcp

推荐答案

您的寻址和内存管理很简单......关闭。首先,绝对没有理由为此创建一个单例,但这不是重点。

Your addressing, and memory management is just plain... off. Firstly, there's absolutely no reason to create a singleton for this, but that's beside the point here.

其次,当声明属性时,(atomic,assign)默认为if没有特别说明,这意味着你的字符串:

Secondly, when declaring properties, (atomic, assign) is defaulted to if not otherwise specified, which means your string:

@property (nonatomic)NSString *passedValue;

是弱酱,很快就能解除分配和销毁。声明复制 strong ,或保留

is weak sauce, ripe for deallocation and destruction at a moments notice. Declare it copy, strong, or retain.

第三,在推送视图控制器中绝对没有对你的单例的引用,但你似乎相信在不同类中命名相同的对象保留它们的值(特别是当#import'ed)。不是这样。您需要引用您的单例并将 [Model sharedModel] .passedText 的值拉入该文本字段。

Thirdly, there's absolutely no reference to your singleton in the pushed view controller, yet you seem to have the belief that objects that are named the same in different classes retain their value (especially when #import'ed). Not so. You need to reference your singleton and pull the value of [Model sharedModel].passedText into that text field.

事实上,我将您的示例分为两行:

In fact, I fixed your sample in two lines:

//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

产生此结果:

这篇关于Obj-C - 如何使用单例在viewcontrollers之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆