-1

在我的项目中,我想在第一次启动应用程序时显示第一次运行视图。为此,我在 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;

编辑我已经发布了完整的代码。也许有别的东西在路上。

4

6 回答 6

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 中的这段代码.m 没有别的......试一试,希望它也适用于你:-)

于 2013-03-07T15:39:05.603 回答
3
 [acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

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

 [acceptButton addTarget:self action:@selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];
于 2013-03-07T14:33:01.447 回答
1

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

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


-(IBAction)buttonClicked:(id)sender
 {
  [self.myView removeFromSuperview];
 }
于 2013-03-07T14:34:30.557 回答
0

问题出[acceptButton addTarget:self action:@selector(buttonClicked:)在线路上。在该调用中更改buttonClicked为您的方法。acceptButton:

于 2013-03-07T14:33:12.183 回答
0
[acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

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

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

[acceptButton addTarget:self action:@selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];
于 2013-03-07T14:36:21.533 回答
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:)
于 2013-03-07T14:45:14.463 回答