2016-07-15 8 views
0

プログラムでUIを作成しようとしています。私の問題は、ビューコントローラは、サブビューが表示されていません。ここでUIViewControllerのサブビューが表示されない

enter image description here

は私がUIを作成しようとしています方法です:

.h

#import <UIKit/UIKit.h> 

@protocol TimerCreatorDelegate <NSObject> 
- (void)timerCreatorControllerDismissedWithString:(NSString *)timerName andTimeInterval:(NSTimeInterval)timeInterval; 
@end 

@interface TimerCreatorController : UIViewController { 

// id timerCreatorDelegate; 
} 

@property (nonatomic, assign) id<TimerCreatorDelegate> timerCreatorDelegate; 

@property (weak, nonatomic) UIDatePicker *timerLength; 
@property (weak, nonatomic) UITextField *timerNameTextField; 
@property (weak, nonatomic) UIButton *createTimerButton; 

//@property (weak, nonatomic) IBOutlet UIDatePicker *timerLength; 
//@property (weak, nonatomic) IBOutlet UITextField *timerNameTextField; 

- (IBAction)createTimer:(id)sender; 

@end 

.m

#import "TimerCreatorController.h" 

@interface TimerCreatorController() 

@end 

@implementation TimerCreatorController 

@synthesize timerCreatorDelegate; 
@synthesize timerNameTextField; 
@synthesize timerLength; 
@synthesize createTimerButton; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor grayColor]; 

    timerNameTextField.placeholder = @"This timer is for:"; 

    timerLength.datePickerMode = UIDatePickerModeCountDownTimer; 

    [createTimerButton setTitle:@"Start Timer" forState:UIControlStateNormal]; 
    [createTimerButton setTitleColor:[UIColor colorWithRed:46/255 green:134/255 blue:53/255 alpha:1] forState:UIControlStateNormal]; 
    createTimerButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20]; 
} 

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

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

- (void)viewWillLayoutSubviews { 
    printf("viewWillLayoutSubviews Called.\n\n"); 
    [self.view addSubview:timerLength]; 
    [self.view addSubview:timerNameTextField]; 
    [self.view addSubview:createTimerButton]; 

    timerLength.translatesAutoresizingMaskIntoConstraints = false; 
    timerNameTextField.translatesAutoresizingMaskIntoConstraints = false; 
    createTimerButton.translatesAutoresizingMaskIntoConstraints = false; 

    timerLength.frame = CGRectMake(0, 0, self.view.frame.size.width, 216); 
    timerNameTextField.frame = CGRectMake((self.view.frame.size.width - 300)/2, timerLength.frame.size.height + 40, 300, 30); 
    createTimerButton.frame = CGRectMake((self.view.frame.size.width - 96)/2, timerNameTextField.frame.origin.y - 40, 96, 36); 

    [NSLayoutConstraint activateConstraints:[NSArray arrayWithObjects: 
              [timerLength.topAnchor constraintEqualToAnchor:self.view.topAnchor], 
              [timerLength.leftAnchor constraintEqualToAnchor:self.view.leftAnchor], 
              [timerLength.rightAnchor constraintEqualToAnchor:self.view.rightAnchor], 
              [timerLength.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], 

              [timerNameTextField.topAnchor constraintEqualToAnchor:timerLength.bottomAnchor], 
              [timerNameTextField.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-37], 
              [timerNameTextField.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:37], 
//            [timerNameTextField.heightAnchor constraintEqualToAnchor:NSLayoutAttributeNotAnAttribute constant:30], 

              [createTimerButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], 
//            [createTimerButton.widthAnchor constraintEqualToAnchor:NSLayoutAttributeNotAnAttribute constant:96], 
//            [createTimerButton.heightAnchor constraintEqualToAnchor:NSLayoutAttributeNotAnAttribute constant:36], 
              [createTimerButton.topAnchor constraintEqualToAnchor:timerNameTextField.bottomAnchor] 
#warning Finish Layout Constraints 
              , nil]]; 
    NSString *timerLengthString = [NSString stringWithFormat:@"%@", NSStringFromCGRect(timerLength.frame)]; 
    NSString *timerNameTextFieldString = [NSString stringWithFormat:@"%@", NSStringFromCGRect(timerNameTextField.frame)]; 
    NSString *createTimerButtonString = [NSString stringWithFormat:@"%@", NSStringFromCGRect(createTimerButton.frame)]; 

    printf("timerLength: %s \n", [timerLengthString UTF8String]); 
    printf("timerNameTextFieldString: %s \n", [timerNameTextFieldString UTF8String]); 
    printf("createTimerButtonString: %s \n", [createTimerButtonString UTF8String]); 
} 

- (IBAction)createTimer:(id)sender { 
    if ([self.timerCreatorDelegate respondsToSelector:@selector(timerCreatorControllerDismissedWithString:andTimeInterval:)]) { 

     [self.timerCreatorDelegate timerCreatorControllerDismissedWithString:timerNameTextField.text andTimeInterval:timerLength.countDownDuration]; 
    } 
    [self dismissViewControllerAnimated:true completion:nil]; 
} 
@end 

私にもかかわらず、私の意見のフレームを設定し、印刷物が0のフレームを表示しています。

+0

サブビューはどこで作成しますか?私はあなたがそれらを使用しようとしているのを見ていますが、あなたが使っているオブジェクトを実際に作成している場所ではありません。それは.Nibファイルですか、実際にはプログラムですべてをやっていますか?なぜあなたのサブビューは「弱い」言及ですか?彼らが何かの助けがなければ、彼らは彼らが作成された後に解放されます... – Putz1103

+0

@ Putz1103、それは私の問題を解決しました。私はプログラムでそれらを作成しようとしています。あなたのコメントは実現しました、私は初期化するのを忘れました! PEBKAC。私も強く参照を変更しました。私は答えを投稿すると言うでしょう。 –

+0

なぜ天国の名前でプログラムですべてを作成するのですか?それは多くの仕事であり、コンテナビューやテーブルビューテンプレートなどのような多くの機能を提供するものではなく、Storyboard/NibベースのUIデザインよりも維持するのがずっと難しくなっています。私がそれをこのように見ることができる唯一の理由については、学習の練習としてです。私が仕事のために誰かにインタビューしている雇用マネージャーで、コードを使ってすべてのUIを行ったと答えた場合、私の返信は「次へ」となります。 –

答えて

1

どこでサブビューを作成しますか?私はあなたがそれらを使用しようとしているのを見ていますが、あなたが使っているオブジェクトを実際に作成している場所ではありません。それは.Nibファイルですか、実際にはプログラムですべてをやっていますか?なぜあなたのサブビューは弱い参照ですか?それらが何かに保持されていない場合、それらは作成された後にリリースされます...

スウィフトでは、オブジェクトを初期化する前に使用しようとするとクラッシュします。 Objective-Cはもう少し寛容であり、少なくともあなたが使用しているインスタンスでは無制限のオブジェクトを使用するように指示すれば何もしません。

関連する問題