如何解除代码生成的子视图?

时间:2013-03-07 14:30:35

标签: ios objective-c uiview uiviewcontroller

在我的项目中,我想在第一次启动应用程序时显示第一个运行视图。为此,我在我的mainViewCntroller.m中添加了一个UIView。为了忽略这个(重叠)视图,我在视图上放了一个名为acceptButton的按钮。我必须添加什么代码才能从Button中删除此视图?

我做错了什么?

这是我的代码:

- (void) firstRun {
if (((AppDelegate*)[UIApplication sharedApplication].delegate).firstRun)
{
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;

    NSLog(@"%f", height);

    CGRect myFrame = CGRectMake(0, 0, width, height);
    UIView *myView = [[UIView alloc] initWithFrame:myFrame];
    myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    myView.tag = 12345;
    [self.view addSubview:myView];

    // Create a ScrollView and a label
     UIScrollView *discScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 0.0f, self.view.frame.size.width - 20, self.view.frame.size.height -70)];
     discScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

     UILabel *discLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
     discLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

     NSString *labelText = NSLocalizedString (@"InfoText",@"");
     [discLabel setText:labelText];

     // Tell the label to use an unlimited number of lines
     [discLabel setNumberOfLines:0];
     [discLabel setNumberOfLines:0];
     [discLabel setBackgroundColor:[UIColor clearColor]];
     [discLabel setFont:[UIFont boldSystemFontOfSize:17]];
     discLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
     //[infoLabel sizeToFit];

     CGSize infoLabelSize = [discLabel.text sizeWithFont:discLabel.font
     constrainedToSize:CGSizeMake(discScroll.frame.size.width, 5000)
     lineBreakMode:UILineBreakModeWordWrap];

     discLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
     discScroll.contentSize = infoLabelSize;

     [discScroll addSubview:discLabel];

     [self.view addSubview:discScroll];

    UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    acceptButton.frame = CGRectMake(110, [[UIScreen mainScreen] bounds].size.height - 74, 100, 44);
    // position in the parent view and set the size of the button
    [acceptButton setTitle:@"Click Me!" forState:UIControlStateNormal];
    // add targets and actions
    [acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // add to a view
    [self.view addSubview:acceptButton];


}
}

-(IBAction)buttonClicked:(id)sender
{

[[self.myView viewWithTag:12345] removeFromSuperview];

}

在我的mainViewController.h中有以下属性:

@property (weak, nonatomic) IBOutlet UIView *myView;

编辑我发布了完整的代码。也许还有其他东西在阻碍。

6 个答案:

答案 0 :(得分:6)

我已经在Xcode中编写了它,以下代码适用于我。

我开了一个新的XCode项目(基于单一视图),刚刚添加了以下代码:

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    myView.tag = 12345;
    [self.view addSubview:myView];

    UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    acceptButton.frame = CGRectMake(0, 0, 100, 44);
    [acceptButton setTitle:@"Click Me!" forState:UIControlStateNormal];
    [acceptButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
    [myView addSubview:acceptButton];

    [myView release];


}

- (void)buttonClicked{
    [[self.view viewWithTag:12345] removeFromSuperview];
}

这对我来说在iPhone模拟器中工作....只需要在MainViewController.m中使用此代码,只需试一试,希望它也适用于你: - )

答案 1 :(得分:3)

 [acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

当您的方法被称为acceptButton时,请更改为

 [acceptButton addTarget:self action:@selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];

答案 2 :(得分:1)

您的目标设置为buttonClicked,但您使用的是acceptButton

[acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];


-(IBAction)buttonClicked:(id)sender
 {
  [self.myView removeFromSuperview];
 }

答案 3 :(得分:0)

问题在于[acceptButton addTarget:self action:@selector(buttonClicked:)行。将buttonClicked更改为该通话中的方法acceptButton:

答案 4 :(得分:0)

[acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

异常很明显,没有名为“buttonClicked”的函数

只需使用以下代码更改代码部分;

[acceptButton addTarget:self action:@selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];

答案 5 :(得分:0)

给它一个标签......

UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
[myView release];

-(IBAction)acceptButton:(id)sender{
[[self.view viewWithTag:12345] removeFromSuperview];
}

同时更改

action:@selector(buttonClicked:)

action:@selector(acceptButton:)