UIViewController 的子视图未显示

Subviews for a UIViewController are not showing

我正在尝试以编程方式创建 UI。我的问题是视图控制器子视图没有显示。

以下是我尝试创建 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},{0,0}}

你在哪里创建子视图?我看到你试图使用它们,但不是在你实际创建你正在使用的对象的地方。它是一个 .Nib 文件还是您实际上以编程方式执行所有操作?为什么你的子视图是弱引用?如果它们没有被其他任何东西持有,它们将在创建后被释放...

在 Swift 中,当您尝试在对象初始化之前使用它时,它会崩溃。 Objective-C 更宽容一点,如果您告诉它使用 nil 对象(至少在您使用的实例中),它什么也不做。