我对Objective-C和iOS编程还很陌生,在传递数据时遇到很多麻烦。我创建了一个模型“LevelsCompleted”,在其中声明了NSString
属性:
@property (readwrite,copy) NSString *answersString;
我在.m文件中合成了它:
@synthesize answersString = _answersString;
现在,如果我在其中一个控制器中更改其值
-(void) changeAnswersString{
LevelsCompleted *lvls = [[LevelsCompleted alloc] init];
[lvls setAnswersString:@"1"];
}
然后从第三个控制器访问它:
-(void) showAnswerStringValue{
LevelsCompleted *lvls = [[LevelsCompleted alloc]init];
NSLog(@"%@", [lvls answersString]);
}
控制台显示null而不是1
我确实找不到为每个控制器更改数据的方法,而且我的英语非常有限,所以我不知道在文档中搜索什么。有什么帮助吗?
实际代码
// PuzzleOneViewController.m
#import "PuzzleOneViewController.h"
#import "LevelChooseViewController.h"
@interface PuzzleOneViewController(){
int thinInt;
int i;
int answer[10];
bool validSubmit, miniPresent, toDelete;
}
@end
@implementation PuzzleOneViewController
@synthesize levels;
- (IBAction)backButton:(id)sender {
if (levels ==nil) self.levels = [[LevelsCompleted alloc]init];
self.levels.answersString=@"Hello";
[self dismissModalViewControllerAnimated:NO];
}
@end
// PuzzleOneViewController.h
#import <UIKit/UIKit.h>
#import "LevelsCompleted.h"
@interface PuzzleOneViewController : UIViewController{
BOOL grosseur;
NSString *choice;
IBOutlet UIScrollView *sv;
IBOutlet UILabel *cluesLabel;
IBOutlet UILabel *hintLabelText;
}
@property (nonatomic, retain) LevelsCompleted *levels;
- (IBAction)backButton:(id)sender;
@end
// LevelsCompleted.h这是我的模型
#import <Foundation/Foundation.h>
@interface LevelsCompleted : NSObject
{
int hello;
BOOL success[30];
NSString *answersString;
}
@property (readwrite,copy) NSString *answersString;
-(BOOL) success:(int)number;
-(void) setSuccess:(BOOL)result atIndex:(int)number;
@end
// LevelsCompleted.m
#import "LevelsCompleted.h"
@implementation LevelsCompleted
@synthesize answersString;
-(BOOL) success:(int)number{
return success[number];
}
-(void) setSuccess:(BOOL)result atIndex:(int)number{
success[number] = result;
}
@end
// LevelChooseViewController.m
#import "LevelChooseViewController.h"
#import "LevelsCompleted.h"
#import "PuzzleOneViewController.h"
@implementation LevelChooseViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void) viewWillAppear:(BOOL)animated{
PuzzleOneViewController *controller = [[PuzzleOneViewController alloc]init];
NSLog(@"%@",controller.levels.answersString); // prints NULL
NSLog(@"%@", PuzzleOneViewController.levels.answersString); // Generates Error
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"PassLevelA"]){
[[segue destinationViewController] setChoice:_levelChoice];
}
}
- (IBAction)Level1Choose:(id)sender {
_levelChoice = [[sender currentTitle]intValue];
[self performSegueWithIdentifier:@"PassLevelA" sender:self];
}
@end
在LevelChooseViewController.h文件中没有任何有趣的事情...
我无法复制所有内容,因为我的控制器每个都有500余行代码,但是我但是所有相关的内容(我希望如此...)非常感谢您的帮助
最佳答案
您没有访问相同的LevelsCompleted对象实例。 [[LevelsCompleted alloc] init]
创建一个新的LevelsCompleted实例。这是与在changeAnswersString方法中创建的LevelsCompleted对象分离的对象。为了从“第三个控制器”访问完成级别的对象,您需要想出一种在两个控制器之间共享数据的方法。在不知道程序整体结构的情况下,很难确切地说出应该做什么,但是一个简单的选择是在第一个控制器或应用程序委托中添加一个“级别” @property,这样您就可以从其他地方:
在您的FirstController.h中:
@property (nonatomic, retain) LevelsCompleted *levels;
在您的FirstController.m中:
@synthesize levels;
然后,在changeAnswersString中:
- (void)changeAnswersString
{
if (self.levels == nil) self.levels = [[LevelsCompleted alloc] init];
self.levels.answersString = @"1";
}
现在,在showAnswerStringValue中:
- (void)showAnswerStringValue
{
NSLog(@"%@", firstController.levels.answersString);
}
与往常一样,您应该仔细考虑程序的结构。不可能像这样一个简短的答案传达所有要考虑的事情,但是我建议您阅读标准的Cocoa设计模式,特别是模型视图控制器(MVC)范例。
关于objective-c - 如何从XCode 4.2中的不同 Controller 的模型更改数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8694854/