gpt4 book ai didi

ios - 使用新对象更新 UIview

转载 作者:行者123 更新时间:2023-11-29 04:40:35 24 4
gpt4 key购买 nike

我有一个 makeButtons 方法(发布在这里),它删除屏幕中的所有按钮并再次添加它们。这在 viewDidLoad 和 viewDidAppear 中工作得很好。我正在从网络服务访问信息,该服务告诉我我需要一个新按钮。当我从该方法调用 [self makebuttons] 时,什么也没有发生,直到我用我的 NavigationController 来回移动,迫使 viewDidAppear 再次完成工作。我的问题是为什么?我正在做完全相同的事情,除非它不是从 viewDidAppear 调用,而是从 didGettingInformation 调用。

- (void) viewDidAppear:(bool) animated {
[self makebuttons]; // Works great!
}

- (void) doneGettingInformation : (ASIFormDataRequest *) request {
NSString *response = [request responseString];
[[self.temp.userInfo objectForKey:@"spillider"] addObject:response];
[self makebuttons]; // This gets called, but nothing changes in the view itself.
}

- (void) makeButtons {
NSLog(@"kjort");
int newAntall = [[self.temp.userInfo objectForKey:@"spillider"] count];
for (UIButton * button in gameButtons) {
NSString *tag = [NSString stringWithFormat:@"%i",button.tag];
[button removeFromSuperview];
if ([webviews objectForKey:tag]) {
[[webviews objectForKey:tag] removeFromSuperview];
[webviews removeObjectForKey:tag];
}
}
[gameButtons removeAllObjects];
scroller.contentSize = CGSizeMake(320, 480);
if (newAntall > 3) {
CGSize scrollContent = self.scroller.contentSize;
scrollContent.height = scrollContent.height+((newAntall-3)*BUTTON_HEIGTH);
self.scroller.contentSize = scrollContent;
}
int y = 163;
self.nyttSpillKnapp.frame = CGRectMake(BUTTON_X, y, BUTTON_WIDTH, 65);
for (int i=0; i<newAntall; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"knapp_midt"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"knapp_midt"] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:15]];
button.frame = CGRectMake(BUTTON_X, y, BUTTON_WIDTH, BUTTON_HEIGTH);
button.enabled = YES;
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(deleteButton:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[button addGestureRecognizer:swipe];
button.tag = [[[self.temp.userInfo objectForKey:@"spillider"] objectAtIndex:i] intValue];
NSString * tittel = [NSString stringWithFormat:@"spill %@",[[self.temp.userInfo objectForKey:@"spillider"] objectAtIndex:i]];
[button setTitle:tittel forState:UIControlStateNormal];
UIButton *subButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
subButton.transform = CGAffineTransformMakeRotation(M_PI_2);
subButton.tag = i;
CGRect subframe = CGRectMake(230, 5, subButton.frame.size.width, subButton.frame.size.height);
subButton.frame = subframe;
CGRect myframe = self.nyttSpillKnapp.frame;
myframe.origin.y = myframe.origin.y+BUTTON_HEIGTH;
self.nyttSpillKnapp.frame = myframe;
[subButton addTarget:self action:@selector(clickGameButton:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:subButton];
[gameButtons addObject:button];
[self.scroller addSubview:button];
y += BUTTON_HEIGTH;
}
}

总而言之,只有当我来回更改 View Controller 导致 viewWillAppear 被调用时,它才有效。这是为什么?

我对我的困惑方法感到抱歉。

谢谢

最佳答案

如果您在初始 View 出现过程或布局更改之外更改了 View 内容,则您有责任调用 setNeedsDisplay 并通知运行循环需要重绘。

系统将要求 View 最初或在布局更改期间绘制其内容,这就是它作为首先显示 View 的过程的一部分的原因。在初始过程中,viewWill/DidAppear 委托(delegate)将被调用。

来自UIView class reference :

The View Drawing Cycle

View drawing occurs on an as-needed basis. When a view is first shown, or when all or part of it becomes visible due to layout changes, the system asks the view to draw its contents. For views that contain custom content using UIKit or Core Graphics, the system calls the view’s drawRect: method. Your implementation of this method is responsible for drawing the view’s content into the current graphics context, which is set up by the system automatically prior to calling this method. This creates a static visual representation of your view’s content that can then be displayed on the screen.

When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay or setNeedsDisplayInRect: method of the view. These methods let the system know that it should update the view during the next drawing cycle. Because it waits until the next drawing cycle to update the view, you can call these methods on multiple views to update them at the same time.

编辑:

此外,请确保不会在后台线程上调用完成获取图像。您无法在后台线程上编辑 View 。如果是,您可以在 bg 线程上准备所有数据,然后在主线程上调用 makeButtons(performSelectorOnMainThread 或使用 block 。

参见GCD, Threads, Program Flow and UI Updating

关于ios - 使用新对象更新 UIview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461624/

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