保存一个ibaction的结果

时间:2014-02-13 01:24:11

标签: objective-c ibaction

现在按下按钮时,它会在不同的视图控制器中创建一个新按钮。但是当你离开那个视图控制器并返回时,创建的按钮就消失了。如何保存该按钮以便它始终存在? 谢谢。

- (IBAction)doneTextField:(id)sender {

       YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
        YSVC2.stringFromTextField = self.textField.text;
        [self presentViewController:YSVC2 animated:YES completion:nil];

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(15, 82, 85, 85);
        [btn addTarget:self action:@selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:nil forState:UIControlStateNormal];
        UIImage *buttonImageNormal = [UIImage imageNamed:@"whatsapp_social_circle-128.png"];
        UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
        [btn setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
        UIImage *buttonImagePressed = [UIImage imageNamed:@"whatsapp_social_circle-128.png"];
        UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
        [btn setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
        [YSVC2 addFaceTimeButton:btn];

    }

    - (void)newButtonClicked
    {
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"tel:9999999999"]];


    }


    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        return [textField resignFirstResponder];
    }

1 个答案:

答案 0 :(得分:0)

问题是每次单击按钮时都会创建一个新的视图控制器实例。

这使得每次都有一个新的视图控制器:

YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"]

如果您需要保持YSVC2的外观,则需要保留对该确切实例的引用。

您可以将YSVC2更改为属性,可以将YourShortcutsViewController转换为单身,您可以在某些常用代码中创建YourShortcutsViewController属性(例如应用代理)...确实有很多方法可以解决这个问题。