在视图控制器

时间:2016-03-07 07:06:15

标签: ios objective-c

我每次启动应用时都会显示一个alertview。当我点击取消然后按钮然后按钮时,我点击了alertview的显示按钮,我想在viewcontroller上显示一个按钮。我正在使用此代码执行此操作。

- (void )alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{

    ViewController *controller = [[ViewController alloc]init];
    controller.button.hidden= NO;
}

并在viewcontroller中创建按钮的出口。并在视图中完成下面的代码加载视图控制器,但我无法显示  按钮

- (void)viewDidLoad 
{
[super viewDidLoad];

self.button.hidden = YES;
}

4 个答案:

答案 0 :(得分:1)

仅在viewDidLoad

中添加此行
self.button.hidden = NO;

你必须替换viewdid load中的代码......

答案 1 :(得分:1)

尝试更改

ViewController *controller = [[ViewController alloc]init];

ViewController *controller = [[ViewController alloc]initWithNibName:nibName];

检查它是否有效!!

答案 2 :(得分:1)

当你创建UIAlertView时,将ViewController(与你的rootViewController相同的实例)设置为它的委托,然后在ViewController中实现

    @Override
public void draw(Graphics2D g) {
    if (imageNum == 0){
        sprite = new Sprite(Sprite.MINE,this.position,new Vector2D(0,0),this.radius,this.radius,0);
    }else if (imageNum == 1){
        sprite = new Sprite(Sprite.MINE1,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 2){
        sprite = new Sprite(Sprite.MINE2,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 3){
        sprite = new Sprite(Sprite.MINE3,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 4){
        sprite = new Sprite(Sprite.EXPLOSION_4,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 5){
        sprite = new Sprite(Sprite.EXPLOSION_5,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 6){
        sprite = new Sprite(Sprite.EXPLOSION_6,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 7){
        sprite = new Sprite(Sprite.EXPLOSION_7,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 8){
        sprite = new Sprite(Sprite.EXPLOSION_6,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 9){
        sprite = new Sprite(Sprite.EXPLOSION_5,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 10){
        sprite = new Sprite(Sprite.EXPLOSION_4,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 11){
        sprite = new Sprite(Sprite.MINE3,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 12){
        sprite = new Sprite(Sprite.MINE2,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum ++;
    }else if (imageNum == 13){
        sprite = new Sprite(Sprite.MINE1,this.position,new Vector2D(0,0),this.radius,this.radius,0);
        this.radius = sprite.getRadius();
        imageNum = 1;
    }
    sprite.draw(g);
}

委托方法。在那里你可以使用self.button

答案 3 :(得分:0)

UIAlertView已弃用。请使用UIAlertController,其首选样式为UIAlertControllerStyleAlert

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

- (void)viewDidLoad {
    [super viewDidLoad];
    //Init hide button
    self.button.hidden = YES;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //Show button
        self.button.hidden = NO;
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //Hide button
        self.button.hidden = YES;
    }];
    [alert addAction:ok];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

您当前的代码可以是这样的:

delegate.m

ViewController *controller = [[ViewController alloc]init];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    //Show button
    controller.button.hidden = NO;
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    //Hide button
    controller.button.hidden = YES;
}];
[alert addAction:ok];
[alert addAction:cancel];
[window.rootViewController presentViewController:alert animated:YES completion:nil];

viewcontroller.m

- (void)viewDidLoad 
{
    [super viewDidLoad];
    //self.button.hidden = NO; remove this line
}