Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 向自定义UIView添加UI元素_Iphone_Objective C_Ios_Uiview - Fatal编程技术网

Iphone 向自定义UIView添加UI元素

Iphone 向自定义UIView添加UI元素,iphone,objective-c,ios,uiview,Iphone,Objective C,Ios,Uiview,我正在尝试添加一些UI元素,如UIProgressView、UILabel、UIButton和UIActivityIndicator——我认为代码是正确的(或者我是这么认为的),但除了背景之外什么都没有显示。我做错了什么 @interface LoadingView : UIView @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; @property (weak, nonatomi

我正在尝试添加一些UI元素,如
UIProgressView
UILabel
UIButton
UIActivityIndicator
——我认为代码是正确的(或者我是这么认为的),但除了背景之外什么都没有显示。我做错了什么

@interface LoadingView : UIView

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

- (void)removeLoadingView;

@end


@implementation LoadingView

@synthesize activityIndicator = _activityIndicator;
@synthesize progressView = _progressView;
@synthesize statusLabel = _statusLabel;
@synthesize cancelButton = _cancelButton;

- (UIView *)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(34, 225, 248, 9)];
        self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(36, 257, 248, 31)];
        self.cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(121, 317, 74, 37)];

        // Create a new image view, from the image made by our gradient method
        UIImageView *background = [[UIImageView alloc] initWithImage:[self addBackground]];
        background.alpha = 0.8;
        [self addSubview:background];

        self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        // Set the resizing mask so it's not stretched
        self.activityIndicator.autoresizingMask =
        UIViewAutoresizingFlexibleTopMargin |
        UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleLeftMargin;

        CGRect indicatorFrame = CGRectMake(142, 163, 37, 37);
        [self.activityIndicator setFrame:indicatorFrame];

        self.statusLabel.text = @"test 123";
        self.statusLabel.hidden = NO;
        self.statusLabel.enabled = YES;
        self.statusLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.activityIndicator];
        [self addSubview:self.progressView];
        [self addSubview:self.cancelButton];
        [self addSubview:self.statusLabel];

        [self.activityIndicator startAnimating];

       /* // Create a new animation
        CATransition *animation = [CATransition animation];
        // Set the type to a nice wee fade
        [animation setType:kCATransitionFade];
        // Add it to the superView
        [[super layer] addAnimation:animation forKey:@"layerAnimation"];*/

    }
    return self;
}



- (UIImage *)addBackground{
    // Create an image context (think of this as a canvas for our masterpiece) the same size as the view
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 1);
    // Our gradient only has two locations - start and finish. More complex gradients might have more colours
    size_t num_locations = 2;
    // The location of the colors is at the start and end
    CGFloat locations[2] = { 0.0, 1.0 };
    // These are the colors! That's two RBGA values
    CGFloat components[8] = {
        0.4,0.4,0.4, 0.8,
        0.1,0.1,0.1, 0.5 };
    // Create a color space
    CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
    // Create a gradient with the values we've set up
    CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
    // Set the radius to a nice size, 80% of the width. You can adjust this
    float myRadius = (self.bounds.size.width*.8)/2;
    // Now we draw the gradient into the context. Think painting onto the canvas
    CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, self.center, 0, self.center, myRadius, kCGGradientDrawsAfterEndLocation);
    // Rip the 'canvas' into a UIImage object
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // And release memory
    CGColorSpaceRelease(myColorspace);
    CGGradientRelease(myGradient);
    UIGraphicsEndImageContext();
    // … obvious.
    return image;
}
编辑:所以我只是把代码放在
viewDidload
中,现在什么都不显示,甚至背景也不显示。我还尝试了
layoutSubviews
中的代码,结果与上面的代码相同,除了背景之外的所有内容都隐藏了

我用来从视图控制器实例化这个视图的代码是

@property (nonatomic, strong) LoadingView *loadingView;

@synthesize loadingView = _loadingView;

- (LoadingView *)loadingView
{
    if (!_loadingView) _loadingView = [[LoadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    return _loadingView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.loadingView];
}

将其放入视图控制器的
viewDidLoad
。这将确保主视图已经存在。

将其放入视图控制器的
viewDidLoad
中。这将确保主视图已经存在。

是否在所需的位置调用了方法
initWithFrame:


我认为您最好在方法
init:
-ViewDidLoad
中添加代码。您是否在需要的位置调用了方法
initWithFrame:


我认为您最好在方法
init:
-ViewDidLoad
中添加代码,然后尝试self.cancelButton.backgroundColor=[UIColor redColor];看看有没有什么发现


默认情况下,使用代码创建的自定义按钮没有背景图像或颜色,您需要手动指定它们。

尝试self.cancelButton.backgroundColor=[UIColor redColor];看看有没有什么发现


默认情况下,使用代码创建的自定义按钮没有背景图像或颜色,您需要手动指定它们。

您应该在
viewDidLoad
-(void)layoutSubview
中执行此操作,如果您正在构建自定义UIView

则应该在
viewDidLoad
-(void)中执行此操作布局子视图
如果要构建自定义UI视图

,则不应将新UI元素添加到self视图,而不仅仅是self视图。所以看起来有点像这样:

 [[self view] addSubview:background];
而不是

 [self addSubview:background];
编辑

尝试此操作以查看控制器上的所有视图

    for(UIView *vw in self.subviews) {
         NSLog(@"View : %@", vw);
    }
如果您无法访问self.subview,这意味着您无法添加除初始视图之外的任何视图,因此您必须开始添加到[self view]。尝试将self.subview更改为[[self view]subview]


您的背景视图可以作为初始视图放置,因为它是UIImageView,请尝试添加到该视图。

如果您不将新的UI元素添加到self视图中,而不仅仅是self视图中。所以看起来有点像这样:

 [[self view] addSubview:background];
而不是

 [self addSubview:background];
编辑

尝试此操作以查看控制器上的所有视图

    for(UIView *vw in self.subviews) {
         NSLog(@"View : %@", vw);
    }
如果您无法访问self.subview,这意味着您无法添加除初始视图之外的任何视图,因此您必须开始添加到[self view]。尝试将self.subview更改为[[self view]subview]



您的背景视图可以作为初始视图放置,因为它是UIImageView,请尝试添加到该视图。

只有在发布
@界面的代码后,我才意识到我正在使用
指针创建元素。我把它改成了
strong
,一切都很好。谢谢大家的回答,我知道我没有显示全部代码,这让你们都很困惑

只有在发布了
@界面的代码之后,我才意识到我是在用
弱的
指针创建元素。我把它改成了
strong
,一切都很好。谢谢大家的回答,我知道我没有显示全部代码,这让你们都很困惑

你能显示d代码吗,为什么你调用这个mthd并在子视图中添加..我尝试了相同的代码,成功地在视图中添加。。如何通过按下/显示来访问此视图控制器。。。因为当alloc调用控制器viewDidLoad时,&当push/present其他视图循环mthds将被调用..你能显示d代码吗,为什么调用此mthd并在子视图上添加..我尝试了相同的代码,成功地在视图上添加。。如何通过按下/显示来访问此视图控制器。。。因为当alloc调用一个控制器viewDidLoad时,&当push/present其他视图循环mthds将被调用..只是尝试将代码放入
viewDidLoad
中,但什么都没有显示,甚至背景也没有显示..太奇怪了。为了确保@Mundi提到将代码放入
viewDidLoad
。这样做,从
initWithFrame
方法中的if语句之间获取代码片段,并将其粘贴到
viewDidLoad
中。只需尝试将代码放入
viewDidLoad
中,就什么也没有显示,甚至背景也没有显示……非常奇怪。要确保@Mundi提到将代码放入
viewDidLoad
。这样做,从
initWithFrame
方法中的if语句之间获取代码片段,并将其粘贴到
viewDidLoad
中。当我将代码放入
viewDidLoad
中时,没有显示任何内容,甚至没有显示背景,当我将其放入
layoutSubviews
中时,它与上面的代码相同,除了背景什么都不显示。当我把代码放在
viewdiload
中时,什么都不显示,甚至背景也不显示,当我把它放在
layoutSubviews
中时,它与上面的代码一样,除了背景什么都不显示。这是一个自定义UIView,所以我想将子视图添加到它本身。Plus没有解释为什么显示背景,但没有显示其他元素。没有
[self view]
代码在
UIView
子类中,而不是
UIViewController
-我尝试添加
NSLog(@“%@”,self.subview)
initWithFrame
方法的底部,唯一显示的是背景。因此,我尝试了
[background addSubview:self.activityIndicator]
但仍然没有显示任何内容。此外,当我删除
[self addSubview:background]
要添加到