iphone - UILabel 未更新与 IBOutlet 连接

标签 iphone objective-c ios ipad cocoa

我正在尝试更新 IBOutlet 的 UILabel,但是由于某种原因,内容没有得到更新。最初,内容从模型中加载,模型异步执行 jsonResponse。但是,一旦内容存在,我就设置了一个委托(delegate)来更新我调用 setNeedsDisplay 的布局。

如有任何帮助,我们将不胜感激。

谢谢。

下面的代码:

@interface pd ()

@property (nonatomic,retain) NSDictionary *pD;
@property (nonatomic, retain) pd_model *pdModel;

@end

@implementation pd

@synthesize pD = _pD, pdModel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil :(NSDictionary*)pD {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        // Custom initialization
        self.productDetails = [[NSDictionary alloc] initWithDictionary:pD];
        self.pdModel = [[ProductDetails_Model alloc] init:pD];
        self.pdModel.delegate = self;
        NSLog(@"%@",[self.pD description]);
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [scrollView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:scrollView];
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.scrollEnabled = YES;

    // Do any additional setup after loading the view from its nib.

    productRecCarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0.0f, scrollView.contentSize.height, 320.0f, 100.0)];
    productRecCarousel.delegate = self;

    [scrollView addSubview:productRecCarousel];

    [self addProductDescription];
    NSLog(@"%@",[self.pdModel responseCode]);
    [soldBy setText:[self.pdModel responseCode]];
    //[soldBy setText:@"1"];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark
#pragma mark CarouselDelegate methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {

    return 1;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel {

    return 1;
}

- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {

    return [[UIView alloc] init];
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel {
    //usually this should be slightly wider than the item views
    return 150;
}

- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset {
    //implement 'flip3D' style carousel

    //set opacity based on distance from camera
    view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);

    //do 3d transform
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = _carousel.perspective;
    transform = CATransform3DRotate(transform, M_PI / 8.0, 0, 1.0, 0);
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * _carousel.itemWidth);
}

- (void)addProductDescription {

    NSLog(@"%f",addProductDetails.frame.size.height);
    addProductDetails.frame = CGRectMake(0.0f, productRecCarousel.frame.size.height, addProductDetails.frame.size.width, addProductDetails.frame.size.height);
    [scrollView addSubview:addProductDetails];


}

- (void)updateTheLayout {

    //[self.view setNeedsDisplay];
   //This is where I get the delegate called once the model is being updated. I tried scrollView and self.view setNeedsDisplay and setNeedsLayout but the IBoutlet never gets updated which is in "ViewDidLoad as assigned as" : [soldBy setText:[self.pdModel responseCode]];

}

谢谢。

最佳答案

所以你只需要从 updateTheLayout 而不是从 viewDidLoad 调用 [soldBy setText:[self.pdModel responseCode]]; 因为它还没有准备好。请确保从主线程更新 UI 元素。

关于iphone - UILabel 未更新与 IBOutlet 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904998/

相关文章:

ios - 如何在 ios 中获取 GCM 通知

ios - 在 Swift 中延迟排序声音

ios - 此按钮上的样式未显示在移动设备 (iOS) 上

iphone - 检测是否触摸了 UIView

iphone - 发送到 MFMessageComposeViewController 的收件人是否可以格式化为显示姓名而不是电话号码?

iphone - 重复scrollView动画

ios - 如何解决多个带有分隔数据的NSURLconnection

ios - [FIRA_AppDelegate- 消息传递 :didRefreshRegistrationToken:]: unrecognized selector sent to instance - firebase crash issue

ios - 错误 ITMS-9000 : “Unsupported architectures. Xcode 存档错误

ios - 在全屏模式下强制横向显示 MPMoviePlayerController 会在退出全屏模式时阻止正确的旋转