init之后类的对象是nil

时间:2014-02-12 11:52:47

标签: ios objective-c

之前,我想为我的英语道歉,我很快就会解决这个问题; - )

所以,我有一个UIView类下降的对象。在initWithFrame中我使用对象的init数组,其他方法数组中的BUT等于nil。我认为这个问题与ARC有关,但不知道为什么以及如何解决它。

请帮助初级开发人员)

TNX !!

是的,我使用xib

代码:

        #import <UIKit/UIKit.h>

        #import "MYRegistrationView.h"

        @interface MYSelectDayView : UIView <RegisterViewDelegate>

        //property
        @property (strong, nonatomic)          NSNumber *weekday;
        @property (strong, nonatomic)          NSMutableArray  *arrayOfButtons;

        //Buttons
        @property (weak, nonatomic) IBOutlet UIButton *mondayButton;
        @property (weak, nonatomic) IBOutlet UIButton *tuesdayButton;
        @property (weak, nonatomic) IBOutlet UIButton *wednesdayButton;
        @property (weak, nonatomic) IBOutlet UIButton *thursdayButton;
        @property (weak, nonatomic) IBOutlet UIButton *fridayButton;
        @property (weak, nonatomic) IBOutlet UIButton *saturdayButton;
        @property (weak, nonatomic) IBOutlet UIButton *sundayButton;

        //Action
        - (IBAction)selectWeekday:(id)sender;

        //methods
        - (void) getDaysOfTraining: (NSArray *) array;



        @end

        #import "MYSelectDayView.h"
        #import "MYRegistrationView.h"

        @implementation MYSelectDayView 

        @synthesize weekday;

        @synthesize arrayOfButtons;

        @synthesize mondayButton;
        @synthesize tuesdayButton;
        @synthesize wednesdayButton;
        @synthesize thursdayButton;
        @synthesize fridayButton;
        @synthesize saturdayButton;
        @synthesize sundayButton;

        - (id)initWithFrame:(CGRect)frame
        {
            self = [super initWithFrame:frame];
            if (self) {
                // Initialization code
            }

            weekday = [[NSNumber alloc] init];


            mondayButton = [[UIButton alloc] init];
            tuesdayButton = [[UIButton alloc] init];
            wednesdayButton = [[UIButton alloc] init];
            thursdayButton = [[UIButton alloc] init];
            fridayButton = [[UIButton alloc] init];
            saturdayButton = [[UIButton alloc] init];
            sundayButton = [[UIButton alloc] init];

            arrayOfButtons = [[NSMutableArray alloc] initWithObjects:mondayButton,
                                                              tuesdayButton,
                                                              wednesdayButton,
                                                              thursdayButton,
                                                              fridayButton,
                                                              saturdayButton,
                                                              sundayButton,      nil];

            return self;
        }

        - (IBAction)selectWeekday:(id)sender {

            weekday =[NSNumber numberWithInt:[sender tag]];

        }

        - (void) getDaysOfTraining: (NSArray *) array {
            for (int i=0; i<7; i++) {
                if ([array objectAtIndex:i]==[NSNumber numberWithBool:1]) {
                    [[arrayOfButtons objectAtIndex:i] setEnabled: YES];
                    NSLog(@"1");
                }
                else {
                    [[arrayOfButtons objectAtIndex:i] setEnabled: NO];
                    NSLog(@"0");
                }
            }
            NSLog(@"OK");
        }
        @end

1 个答案:

答案 0 :(得分:1)

因为您使用IBOutlet我希望您的视图是在XIB或故事板中创建的。因此,不会调用initWithFrame:(而是调用initWithCoder:来取消归档视图)。您也不应该创建一组全新的按钮(没有目标/动作/标题,也不会被添加为子视图)。

删除initWithFrame:并替换为:

- (void)awakeFromNib
{
    self.arrayOfButtons = [[NSMutableArray alloc] initWithObjects:mondayButton,
                                                                  tuesdayButton,
                                                                  wednesdayButton,
                                                                  thursdayButton,
                                                                  fridayButton,
                                                                  saturdayButton,
                                                                  sundayButton,
                                                                  nil];
}