objective-c - 在我的模型中声明一个数组以在控件中使用

标签 objective-c ios xcode4

我是 Objective-C 和 MVC 的新手。我一直在关注 Paul Haggerty 的类(class)作业和讲座,学到了很多东西。我正在进入编程阶段,我实际上能够坐下来编写工作应用程序,而不是仅仅阅读有关 iOS 开发的内容。

我很难理解如何正确使用 MVC。

这是我编写并“工作”的(非常基本的)代码:

- (IBAction)buttonClicked {
    NSArray *namesArray = [NSArray arrayWithObjects:
                           (NSString *)@"Tiffany",
                           (NSString *)@"Jason",
                           (NSString *)@"Mustafa",
                           (NSString *)@"Mellisa",
                           (NSString *)@"Michael",
                           (NSString *)@"Kasim",
                           nil];

    if ([self.myDisplay.text
         isEqualToString:[namesArray objectAtIndex:0]]){
        self.myDisplay.text = [namesArray objectAtIndex:1];
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:1]]){
        self.myDisplay.text = [namesArray objectAtIndex:2];
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:2]]){
        self.myDisplay.text = [namesArray objectAtIndex:3];
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:3]]){
        self.myDisplay.text = [namesArray objectAtIndex:4]; 
    } else if ([self.myDisplay.text 
         isEqualToString:[namesArray objectAtIndex:4]]){
        self.myDisplay.text = [namesArray objectAtIndex:5];
    } else {
        self.myDisplay.text = [namesArray objectAtIndex:0];
    }

    self.numberOfLetters.text = [NSString stringWithFormat:@"%@ Letters", [NSString stringWithFormat:@"%d", self.myDisplay.text.length - 1]];
 }

正如您所看到的,它设置了一个数组,然后当用户单击屏幕上的按钮时,它会显示下一个名称以及该名称中的字母数。我想要做的是在我的模型中创建数组,然后通过 Controller 访问它(这就是你应该做的对吗?)

我的方法是创建一个模型类(我称之为 Brain)。我将大脑导入到 Controller 中,并在大脑中创建了一个 NSArray 属性并合成了它。但我在 Controller 中访问它时遇到困难。

此外,我知道我现在完成的方式是错误的,因为每次用户单击按钮时我基本上都会重新创建数组。

有人可以指导我吗? (顺便说一句,我正在使用 ARC。)

这是我创建“大脑”类(class)的方式:

#import <Foundation/Foundation.h>

@interface Brain : NSObject
@property (nonatomic, strong) NSMutableArray *myNamesArray;

@end

#import "Brain.h"

@implementation Brain
@synthesize myNamesArray = _myNamesArray;

@end

最佳答案

我只会跟踪索引。将其定义为类 header 中的属性。另外,您可能不想继续创建新数组。

@property (nonatomic) int index;
@property (nonatomic, retain) NSArray *namesArray;

在您的实现中综合它:

@synthesize index, namesArray;

在 init 方法(或 viewDidLoad 方法,如果您愿意)中创建数组:

self.namesArray = [NSArray arrayWithObjects:
                       @"Tiffany",
                       @"Jason",
                       @"Mustafa",
                       @"Mellisa",
                       @"Michael",
                       @"Kasim",
                       nil];

如果您不使用 ARC,请务必在 dealloc 方法中释放 namesArray。

然后用它来设置文本字符串。

- (IBAction)buttonClicked {
    self.index++;
    self.myDisplay.text = [self.namesArray objectAtIndex:index%[namesArray count]];
}

关于objective-c - 在我的模型中声明一个数组以在控件中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9402382/

相关文章:

iphone - 在进入后台和在前台检索时将 NSTimer 的时间保存到磁盘

ios - 双导航栏

xcode - 如何在主项目Xcode 4.1中包含 bundle 软件

ios - 适用于 iOS 的 UITableView

objective-c - XCode:为什么我的事件没有被添加到日历中?

ios - 从 Parse iOS 下载图像

ios - 如何动态滚动到列表框的顶部

ios - 如何以编程方式确定 iOS 应用程序是来自应用程序商店还是在开发过程中已复制到设备?

ios - 想要一个下拉到激活单元格下方的单元格

iphone - 我可以在 XCode 4.3.2 中禁用 "Upgrade debugger from GDB to LLDB"警告吗?