好吧,这是我昨晚问的问题的延伸.我对如何使用各种技术在视图控制器之间传递数据有一点了解.我想进入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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Model.h
#import <Foundation/Foundation.h>
@interface Model : NSObject {
NSString *passedText;
}
@property (nonatomic, strong) NSString* passedText;
+ (Model *) sharedModel;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
该项目可以从这里完整下载http://chrisrjones.com/files/KegCop-Test.zip
如果您知道为什么UILabel没有显示文本字段文本,请告诉我.哦,我几乎跟着这个 - > http://www.youtube.com/watch?v=ZFGgMPcwYjg&feature=plcp
你的寻址和内存管理很简单......关闭.首先,绝对没有理由为此创建单例,但这不是重点.
其次,在声明属性时,(atomic,assign)默认为如果没有另外指定,这意味着你的字符串:
@property (nonatomic)NSString *passedValue;
Run Code Online (Sandbox Code Playgroud)
是一种薄弱的酱汁,在瞬间注意到成熟的解除分配和破坏.声明它copy,strong或retain.
第三,在推送视图控制器中绝对没有引用你的单例,但你似乎相信在不同类中命名相同的对象保留它们的值(特别是在#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
Run Code Online (Sandbox Code Playgroud)
这产生了这个:

| 归档时间: |
|
| 查看次数: |
5640 次 |
| 最近记录: |