【问题标题】:Is there a way to call the subview of a view controller's self.view in a function outside of viewDidLoad?有没有办法在 viewDidLoad 之外的函数中调用视图控制器 self.view 的子视图?
【发布时间】:2021-08-25 01:07:58
【问题描述】:

基本上我在这里要做的是有一个函数,它将 self.view 的子视图的 alpha 设置为 1(默认情况下它是隐藏的),它在按下按钮时被调用。我遇到过几次这个问题,因为我在 viewDidLoad 中声明了子视图,但我不知道如何与不同函数中的子视图进行交互(我试过 self.view.subviews[n] 但 xcode 不是对此感到高兴)。我能想到的唯一另一种方法是将self.view设置为我想要的视图,或者甚至可以在我希望用按钮调用的函数中创建视图(我看到的问题可能是视图不会覆盖我想要发生的按钮)。

这是我目前所拥有的:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *firstBackground = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIView *blackOverlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    blackOverlay.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    firstBackground.backgroundColor = [UIColor whiteColor];
    UIButton *fakeNotifButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [fakeNotifButton addTarget:blackOverlay action:@selector(setupFakeNotifScreen:) forControlEvents:UIControlEventTouchUpInside];
    //i haven't finished the button yet, that i don't need any help with
}

-(IBAction)setupFakeNotifScreen:(id)sender {
    [UIView animateWithDuration:1.0
                          delay:0.5
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{view = 1.0;} //here is where i want to call blackOverlay
                     completion:^(BOOL finished){}];
}

@end

编辑:我想通了...甚至不需要 blackOverlay 视图。这是任何未来谷歌人的解决方案......可能有更有效的方法来做到这一点,所以我会感谢任何比我更好的人:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    UIButton *fakeNotifButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [fakeNotifButton addTarget:self action:@selector(setupFakeNotifScreen:) forControlEvents:UIControlEventTouchUpInside];
    fakeNotifButton.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width/2) - 80, 300, 160, 40);
    [fakeNotifButton setTitle:@"test" forState:UIControlStateNormal];
    [fakeNotifButton setBackgroundColor:[UIColor systemGreenColor]];
    fakeNotifButton.layer.cornerCurve = @"continuous";
    fakeNotifButton.layer.cornerRadius = 15;
    [self.view addSubview:fakeNotifButton];
    

}

-(void)setupFakeNotifScreen:(UIButton *)sender {
    [UIView animateWithDuration:0.2
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{self.view.backgroundColor = [UIColor blackColor]; sender.alpha = 0.0;}
                     completion:^(BOOL finished){}];

}

@end

【问题讨论】:

  • 请不要标记垃圾邮件。仅使用您实际编码的语言标签。
  • kaylum 很糟糕,那些在建议的标签下,所以我认为没关系

标签: ios objective-c iphone


【解决方案1】:

当你声明时

UIView *blackOverlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

...该引用在方法viewDidLoad 内,因此它的存在仅限于该方法。它仅在方法内部可见,并且当方法结束时引用确实不存在了。 (视图本身也已不存在;您还没有将其放入界面中。)

如果你想要一个 persistent 对该视图的引用,你需要声明一个 property(或至少一个实例变量,aka iva),并将其设置为此看法。如果您实际上没有将视图放入界面中,您仍然无法对视图执行任何可见的操作。

猜你喜欢
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多