ios - appdelegate.m 中 View Controller 的 ivar

标签 ios initialization storyboard

编辑 1

在尝试使用 codeInOrange 后,我正在添加一些代码来指示其状态到目前为止的答案与我的代码最初的行为相似,即示例链接首先显示在文本字段中并且可以由用户更改,但是当用户返回 VC 时,任何新的链接文本都已替换为原始示例链接。我发布此附加代码的原因是尝试重新连接 codeInOrange的有希望的答案,因为我误解了他最初的建议和他后来的评论的逻辑流程。

在当前的 Storyboard中,我将文本字段和占位符文本留空,因为 viewDidLoad 似乎已充分提供示例链接。下面的方法。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.urlNameInput.text = @"sample http";
    // Do any additional setup after loading the view, typically from a nib.
    self.urlNameInput.clearButtonMode = UITextFieldViewModeAlways;
    self.urlNameInput.clearsOnBeginEditing = NO;   
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField == self.urlNameInput) {
        [textField resignFirstResponder];
        [self processPbn];
    }
    return YES;
}


- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

    // self.urlNameInput.text = appDelegate.stringForTextField;
    appDelegate.stringForTextField = self.urlNameInput.text;
}

- (void) processPbn
{
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlNameInput.text] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    [NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *connection, NSData *data, NSError *error)
     {
         // lots of detail code has been elided in this method 
         self.iboard = 0;
         NSRegularExpression *regex = [NSRegularExpression  regularExpressionWithPattern:toMatch options:NSRegularExpressionDotMatchesLineSeparators error:&error];
         for (NSTextCheckingResult* board in [regex matchesInString:string options:NSRegularExpressionDotMatchesLineSeparators range:NSMakeRange(0, [string length])])
         {

         if (self.iboard>0) {
             AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

             appDelegate.stringForTextField = self.urlNameInput.text;
         }
     }];
}

编辑 1

编辑 0

我不想在应用程序关闭和启动之间保留文本,所以答案使用 NSUserDefaults不是我需要的。

此外,从我的试验看来,Michael Dautermann 建议的解决方案建议将我的初始化文本放在 viewDidLoad 中。或在 Xib 或 Storyboard 中,不起作用,因为文本在返回 VC 时总是返回到其初始值(可能是因为触发了 viewDidLoad 方法),所以我想我做 需要在我的 AppDelegate.m 中创建一个 ivar正如我在原始问题中而不是在我的 ViewController.m viewDidLoad 中所问的那样, 得到想要的结果 , 显然。也许在 AppDelegate.m 中创建一个 B00L ivar 会更容易,它是一个标志,指示是否需要原始文本或当前文本。但我也不知道该怎么做。因此,请在您的答案中考虑此编辑。

编辑 0

我的 AppDelegate.m 包含以下代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        BDViewController *vc = [sb instantiateInitialViewController];
        self.viewController = (id)vc;
    }
    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];

    return YES;
}

在 VC 中,我希望在启动时设置一个 ivar,一个 NSString,以便它可以是我的 UITextField 中的示例文本。稍后,当用户向 UITextField 提供有效文本时,我希望将 UITextField 调整为新值。

目前在我的 VC.h 中,声明文本字段并在 VC.m 中合成如下。
@property (nonatomic, strong)  UITextField *urlNameInput;


@synthesize urlNameInput;

我尝试将以下代码放入 didFinishLaunchingWithOptions:但是当我运行应用程序时看不到所需的文本。
    self.viewController.urlNameInput.text = @"example http";

如何以编程方式完成初始化 UITextField 的目标?

最佳答案

将“urlNameInput.text =”位放入您的 View Controller 的“viewDidLoad”方法,而不是“didFinishLaunchingWithOptions:”方法(您的 View Controller 可能还没有被实例化。

甚至更好的是,只需在 Storyboard或 XIB 文件中设置初始文本,然后您可以稍后以编程方式对其进行调整。

关于ios - appdelegate.m 中 View Controller 的 ivar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18263040/

相关文章:

iphone - iOS 框架无法创建对象

perl - 为什么这个 Perl 变量保持它的值

objective-c - UISplitViewController - 静态主 TableView 和多个详细 View Controller

ios - 在 iOS Interface Builder 中使用 StoryBoard 支持横向和纵向

ios - Phonegap iOS 版本不显示背景图片

iOS - iPad 的 viewWillTransition 中错误的 UIScreen 边界

ruby - 使用哈希参数的 DRY Ruby 初始化

c++ - 使用 { } 的 Visual Studio 2012 C++ 数组初始化

ios - 如何关闭我用 "replace"segue 打开的 View Controller

ios - 如何减少 iOS 上的视频处理时间?