使用情节提要板时,如何在Objective-C中实现自定义init方法?

罗尼普

我使用情节XCode 4.6.3提要创建了一些按钮和标签,我想单击FirstViewController中的按钮时触发一些方法,为了使这些方法起作用,我定义了一些变量/ NSMutableArrays(currentQuestionIndex,questions等)。我想使用自定义的初始化方法来初始化这些。当我离开initWithNibName并按initWithCoder原样编写新的init方法并在其中编写实现时,它不会被调用。

但是,当我按下面的代码片段所示操作代码时,它起作用了。

我想知道在使用创建对象时如何使用自定义init方法,Storyboard因为我在这里所做的可能不是一个好习惯。当我尝试使用初始化时initWithCoder,它不起作用。但是据我所记得,我们使用initWithCoder从初始化Storyboard,对吗?中的按钮/标签storyboard位于FirstViewController中。

这是我的FirstViewController.h文件

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
{
    int currentQuestionIndex;
    // The model objects
    NSMutableArray *questions;
    NSMutableArray *answers;

    //The view objects
    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;
}

@property (strong, nonatomic) UIWindow *window;

- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;

@end

这是我的FirstViewController.m文件

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)init {
    // Call the init method implemented by the superclass
    self = [super init];
    if(self) {
        currentQuestionIndex = -1;
        // Create two arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];
        answers = [[NSMutableArray alloc] init];
        // Add questions and answers to the arrays
        [questions addObject:@"What is 7 + 7?"];
        [answers addObject:@"14"];
        [questions addObject:@"What is the capital of Vermont?"];
        [answers addObject:@"Montpelier"];
        [questions addObject:@"From what is cognac made?"];
        [answers addObject:@"Grapes"];
    }
    // Return the address of the new object
    return self;
}

-(IBAction)showQuestion:(id)sender
{
    currentQuestionIndex++;
    if(currentQuestionIndex == [questions count])
        currentQuestionIndex = 0;
    NSLog(@"%d",[questions count]);
    NSString *question = [questions objectAtIndex:currentQuestionIndex];
    NSLog(@"dislaying question at index %d : %@" ,currentQuestionIndex,question);

    [questionField setText:question];

    [answerField setText:@"???"];
}

-(IBAction)showAnswer:(id)sender
{
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];
    [answerField setText:answer];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
苏希特·帕蒂尔

我会得到所有的值..当我登录...尝试这个

-(id)initWithCoder:(NSCoder *)aDecoder{

    if(self = [super initWithCoder:aDecoder]) {

        currentQuestionIndex = 15;
        NSLog(@"%d", currentQuestionIndex);
        // Create two arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];
        answers = [[NSMutableArray alloc] init];
        // Add questions and answers to the arrays
        [questions addObject:@"What is 7 + 7?"];
        [answers addObject:@"14"];
        [questions addObject:@"What is the capital of Vermont?"];
        [answers addObject:@"Montpelier"];
        [questions addObject:@"From what is cognac made?"];
        [answers addObject:@"Grapes"];
        NSLog(@"Questions = %@", questions);
        NSLog(@"Answers = %@", answers);
     }
     return self;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用情节提要板在UIViewController中初始化自定义UIView

来自分类Dev

如何在iOS7中使用情节提要传递图像

来自分类Dev

从代码和情节提要中实例化自定义按钮-如何创建init方法

来自分类Dev

如何使用情节提要添加自定义数据源类

来自分类Dev

如何使用情节提要从自定义uitableviewcell正确启动弹出窗口选择

来自分类Dev

使用情节提要板实现自定义视图布局

来自分类Dev

如何在Xcode中隐藏情节提要板概述?

来自分类Dev

如何在两个情节提要板上执行自定义搜索?

来自分类Dev

只要init调用[super init],在Objective C自定义init方法中调用[self init]是否可以?

来自分类Dev

只要init调用[super init],在Objective C自定义init方法中调用[self init]是否可以?

来自分类Dev

使用情节提要板仅显示两列,在CollectionView中显示多行

来自分类Dev

使用情节提要中的重叠对象

来自分类Dev

如何在视图控制器中显示不同的视图以进行调试,而不是在iOS上发布(使用情节提要)

来自分类Dev

使用情节提要中的自定义UIView捕获视图,什么也不返回?

来自分类Dev

如何使用xcode 6中的通用情节提要板对uiview的宽度与iphone和ipad进行一些填充以使其适合父视图的宽度?

来自分类Dev

如何在C ++中实现自定义std集合?

来自分类Dev

情节提要和自定义init

来自分类Dev

使用情节提要时未调用UITableViewController替代init函数

来自分类Dev

自定义单元永远不会为零(不要使用情节提要)

来自分类Dev

无法使用情节提要自定义实例化窗口控制器

来自分类Dev

与自定义UITabbarController一起使用情节提要

来自分类Dev

自定义单元永远不会为零(不要使用情节提要)

来自分类Dev

自定义视图过渡,无需使用情节提要脚本(快速)

来自分类Dev

如何使用情节提要给出位置坐标以在ios中的地图中绘制点?

来自分类Dev

将自定义情节提要板设置为主界面时出错

来自分类Dev

如何/是否在Xcode中制作通用情节提要

来自分类Dev

在特定时间使用情节提要调用方法

来自分类Dev

在特定时间使用情节提要调用方法

来自分类Dev

在UICollectionViewCell中使用情节提要中的自动布局约束

Related 相关文章

  1. 1

    使用情节提要板在UIViewController中初始化自定义UIView

  2. 2

    如何在iOS7中使用情节提要传递图像

  3. 3

    从代码和情节提要中实例化自定义按钮-如何创建init方法

  4. 4

    如何使用情节提要添加自定义数据源类

  5. 5

    如何使用情节提要从自定义uitableviewcell正确启动弹出窗口选择

  6. 6

    使用情节提要板实现自定义视图布局

  7. 7

    如何在Xcode中隐藏情节提要板概述?

  8. 8

    如何在两个情节提要板上执行自定义搜索?

  9. 9

    只要init调用[super init],在Objective C自定义init方法中调用[self init]是否可以?

  10. 10

    只要init调用[super init],在Objective C自定义init方法中调用[self init]是否可以?

  11. 11

    使用情节提要板仅显示两列,在CollectionView中显示多行

  12. 12

    使用情节提要中的重叠对象

  13. 13

    如何在视图控制器中显示不同的视图以进行调试,而不是在iOS上发布(使用情节提要)

  14. 14

    使用情节提要中的自定义UIView捕获视图,什么也不返回?

  15. 15

    如何使用xcode 6中的通用情节提要板对uiview的宽度与iphone和ipad进行一些填充以使其适合父视图的宽度?

  16. 16

    如何在C ++中实现自定义std集合?

  17. 17

    情节提要和自定义init

  18. 18

    使用情节提要时未调用UITableViewController替代init函数

  19. 19

    自定义单元永远不会为零(不要使用情节提要)

  20. 20

    无法使用情节提要自定义实例化窗口控制器

  21. 21

    与自定义UITabbarController一起使用情节提要

  22. 22

    自定义单元永远不会为零(不要使用情节提要)

  23. 23

    自定义视图过渡,无需使用情节提要脚本(快速)

  24. 24

    如何使用情节提要给出位置坐标以在ios中的地图中绘制点?

  25. 25

    将自定义情节提要板设置为主界面时出错

  26. 26

    如何/是否在Xcode中制作通用情节提要

  27. 27

    在特定时间使用情节提要调用方法

  28. 28

    在特定时间使用情节提要调用方法

  29. 29

    在UICollectionViewCell中使用情节提要中的自动布局约束

热门标签

归档