當我使用我的url方案啓動應用程序時,我正努力預填一個文本字段。當應用程序沒有在內存中啓動時,值沒有被設置(或者我認爲被viewDidLoad
或類似的推翻了)。從UrlScheme設置ViewController的值
的歸結路上我考慮這個樣子的:
// AppDelegate.m
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
UINavigationController *nav = (UINavigationController *)[[application keyWindow] rootViewController];
MainViewController *main = (MainViewController *)[nav topViewController];
[main setLabelText:@"this should be shown on screen"];
return YES;
}
隨着ViewController
坐在UINavigationController
// MainViewController.m
@interface MainViewController()
@property (weak, nonatomic) IBOutlet UILabel *someLabel;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.someLabel.text = @"this is actually shown on screen";
}
- (void)setLabelText:(NSString *)text
{
self.someLabel.text = text;
}
@end
所以標籤顯示「這實際上是在屏幕上顯示」,而不是我在AppDelegate
中設置的文字。在設置斷點時,我認爲這很明顯,因爲viewDidLoad
在setLabelText
之後被調用。
是否有一個更強大的路徑來預先填寫我缺少的自定義網址方案中的文本字段?
當應用程序在後臺運行,則可以使用一個NSNotification,以讓你的ViewController知道,當你的應用程序使用自定義url方案打開。 – beeef
「已經運行」的情況下,我的運輸應用程序正常工作(不是在簡單的秒殺項目,但不知道爲什麼)。這是造成麻煩的「新發布」。 – user1777136