现在按下按钮时,它会在不同的视图控制器中创建一个新按钮。但是当你离开那个视图控制器并返回时,创建的按钮就消失了。如何保存该按钮以便它始终存在? 谢谢。
- (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];
}
答案 0 :(得分:0)
问题是每次单击按钮时都会创建一个新的视图控制器实例。
YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"]
如果您需要保持YSVC2
的外观,则需要保留对该确切实例的引用。
您可以将YSVC2
更改为属性,可以将YourShortcutsViewController
转换为单身,您可以在某些常用代码中创建YourShortcutsViewController
属性(例如应用代理)...确实有很多方法可以解决这个问题。