UIViewController init方法里面调用self.view的问题


【在UIViewController的init方法中对self.view进行设置需要谨慎】

今天在项目中用到了一个UIViewController的子类,重写了其初始化方法,并在初始化方法中通过self.view setFrame:方法对其大小进行了修改。结果界面上的内容都没有了,纠结了半天才恍然大悟,血的教训啊!!!

事情是这样子的:
以下是纯记事本写的代码,伪码算是。
@interface ViewControllerA:UIViewController
{
}
@property(nonamic,retain)ClassB * classB;

@implementation ViewControllerA
@sythesize classB;

-(id)initWithAClassB:(ClassB *)b //假如传进来的b的value属性=10;
{
if(self = [super init])
{
[self.view setFrame:CGRectMake(0,0,100,100)];
self.classB = b;
//调用其他方法
NSLog(@"when init b.value = %d",self.classB.value);
}
return self;
}

-(void)viewDidLoad
{
[super viewDidLoad];
[self addSubViews];
//调用其他方法
NSLog(@"when didload b.value = %d",self.classB.value);
//此处调用的方法用到了self.classB.value值,但是每次运行都发现传进来的值并不是10,而是默认的0
}

-(void)addSubViews
{
//代码块
NSLog(@"when addSubViews b.value = %d",self.classB.value);
}

结果输出了:
when addSubViews b.value = 0
when didload b.value = 0
when init b.value = 0

===================================================================
我就纳闷儿了,明明是先init再didload的呀,为什么输出顺序是这样子的呢?
后来好好仔细检查了一遍代码,发现在self.classB = b;赋值之前调用了self.view,这个时候self.view还没有,所以要去先加载view,所以导致了这种情况。以前瞎写,完全没有注意到有这个问题,今天涨姿势了。。。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,可以创建一个可缩放的 UIView,并确保它的尺寸不会小于父视图: 在 .h 文件中声明 UIPinchGestureRecognizer 和一个指向父视图的 UIView 属性: ``` #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (nonatomic, strong) UIPinchGestureRecognizer *pinchGesture; @property (nonatomic, strong) UIView *parentView; @end ``` 在 .m 文件中实现 UIPinchGestureRecognizer 和 UIView 的创建和缩放逻辑: ``` #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIView *resizableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建父视图 self.parentView = [[UIView alloc] initWithFrame:self.view.bounds]; self.parentView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:self.parentView]; // 创建可缩放的视图 self.resizableView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; self.resizableView.backgroundColor = [UIColor redColor]; [self.parentView addSubview:self.resizableView]; // 创建缩放手势识别器 self.pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; [self.resizableView addGestureRecognizer:self.pinchGesture]; } - (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { // 计算缩放比例 CGFloat scale = gestureRecognizer.scale; // 计算缩放后的视图大小 CGSize newSize = CGSizeMake(self.resizableView.frame.size.width * scale, self.resizableView.frame.size.height * scale); // 判断缩放后的大小是否小于父视图的大小 if (newSize.width >= self.parentView.frame.size.width && newSize.height >= self.parentView.frame.size.height) { // 缩放视图 self.resizableView.transform = CGAffineTransformScale(self.resizableView.transform, scale, scale); // 重置手势识别器的缩放比例 gestureRecognizer.scale = 1.0; } } } @end ``` 在这个示例中,创建了一个父视图 self.parentView 和一个可缩放的子视图 self.resizableView,并将子视图添加到父视图中。然后创建了一个 UIPinchGestureRecognizer 对象 self.pinchGesture,并将它添加到子视图中。 在 handlePinchGesture: 方法中,计算出缩放比例和缩放后的视图大小,并判断缩放后的大小是否小于父视图的大小。如果缩放后的大小不小于父视图的大小,就缩放视图,并重置手势识别器的缩放比例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值