gpt4 book ai didi

ios - UIViewController 的 subview 未显示

转载 作者:行者123 更新时间:2023-11-29 00:48:18 25 4
gpt4 key购买 nike

我正在尝试以编程方式创建一个 UI。我的问题是 View Controller subview 没有显示。

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

尽管我设置了 View 的框架,但打印件显示的是 {{0,0},{0,0}} 的框架。

最佳答案

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

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

关于ios - UIViewController 的 subview 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38399493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com