2014-02-12 62 views
-1

之前,我要爲我的英語道歉,我會解決這個問題很快;-)對象是零初始化後,

所以,我從UIView類血統的對象。在initWithFrame中,我使用對象初始化數組,但在其他方法數組中等於零。我認爲這個問題與ARC有關,但不知道爲什麼以及如何解決它。

請,幫助初級開發者)

TNX!

是的,我用的是廈門國際銀行

代碼:

 #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 
+3

也請添加相關代碼。 –

+0

顯示相關的代碼 –

+0

顯示init方法和你在哪裏調用它(或者你使用XIB/storyboard)? – Wain

回答

1

由於您使用IBOutlet我期待你的觀點是在廈門國際銀行或情節串連圖板創建。因此,initWithFrame:將不會被調用(將調用initWithCoder:來取消存檔視圖)。你也不應該創建一組全新的按鈕(它沒有目標/動作/標題,也不會添加爲子視圖)。

刪除您initWithFrame:並替換爲:

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