내 appDelegate에서 내 viewController에 어떻게 액세스합니까? iOS
xCode에서 "보기 기반 앱"으로 만든 iOS 앱이 있습니다. viewController가 하나만 있지만 자동으로 표시되고 내 appDelegate에 연결하는 코드가 표시되지 않습니다. 내 appDelegate에서 내 viewController로 데이터를 전달해야하는데 그 방법을 모르겠습니다.
내 앱 delegate.h :
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSDictionary *queryStrings;
@end
또한 appDidFinishLoadingWithOptions :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[JMC sharedInstance] configureJiraConnect:@"https://cmsmech.atlassian.net/" projectKey:@"WTUPLOAD" apiKey:@"7fc060e1-a795-4135-89c6-a7e8e64c4b13"];
if ([launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] != nil) {
NSURL *url = [launchOptions objectForKey: UIApplicationLaunchOptionsURLKey];
NSLog(@"url received: %@", url);
NSLog(@"query string: %@", [url query]);
NSLog(@"host: %@", [url host]);
NSLog(@"url path: %@", [url path]);
queryStrings = [self parseQueryString:[url query]];
NSLog(@"query dictionary: %@", queryStrings);
}
else {
queryStrings = [self parseQueryString:@"wtID=nil"];
}
return YES;
}
다음을 사용하여 액세스 할 수 있습니다.
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
tabviewcontroller 또는 내비게이션 컨트롤러 뒤에 뷰를 중첩하는 경우 해당 뷰를 반환하고 그 안의 뷰 컨트롤러에 액세스해야합니다.
좋은 구식 NSNotifications 를 사용 하여 앱 델리게이트로부터 업데이트가 필요하다는 메시지를 듣고있는 모든 사람 (예 : 뷰 컨트롤러)에게 메시지를 보내는 것은 어떻습니까? 또는 키 값 관찰을 사용 하여 뷰 컨트롤러가 앱 델리게이트의 일부 속성을 감시 할 수 있습니다.
뷰 컨트롤러가 하나뿐이므로 일반적인 방법 (앱 설정 방법과 무관) :
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
당신이 다른 것을 얻고 싶다면 UIViewController,뿐만 아니라 rootViewController:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];
UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];
신속한 방법으로 appdelegate뿐만 아니라 어디서나 호출 할 수 있습니다.
/// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
public var topMostVC: UIViewController? {
var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController {
presentedVC = pVC
}
if presentedVC == nil {
print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.")
}
return presentedVC
}
다음과 같은 표준 기능으로 포함됩니다.
https://github.com/goktugyil/EZSwiftExtensions
If you used the View-Based Application template, the single view controller should be accessible via a property in your app delegate. It's the same view controller that gets set as the root view controller of the navigation controller.
If for some reason your project was set up differently, you can get the root view controller of the window, which should be the navigation controller, and then get its top view controller.
EDIT: So the issue is this: With iOS5 and storyboards, Apple has abstracted a lot of the initial set up that you used to have access to (and still do if you opt out of storyboards). They've altered the arguments passed to main() and do more of the set up in the storyboard (which is really just a nib). IMHO, this is in part to keep people from overloading the AppDelegate with heavy operations, like it appears you are doing in yours. The AppDelegate, in essence, exists to manage the Application lifecycle. It's not really meant to be performing methods that hand off information to your view controllers. It's your view controller's job, really, to do the heavy lifting to provide itself with data. I would suggest moving the code into your view controller, perhaps in viewDidLoad. If you need to know the result of the launchOptions objectForKey test it appears you're doing, you could very simply create a property on your AppDelegate, say BOOL launchOptionsURLKeyExists, and set it appropriately. Then you just grab the value of this property in your view controller. You AppDelegate is already a singleton, so you can access it either by [UIApplication sharedApplication].delegate
EDIT 2: viewWillAppear/viewDidAppear gets called when the view is added to the UIApplicationDidEnterForegroundNotification notification in your view controller and respond appropriately when that message is posted.
'Program Club' 카테고리의 다른 글
| Rails3에서 관련 관계에 범위가 지정되지 않은 것을 사용하는 방법은 무엇입니까? (0) | 2020.11.26 |
|---|---|
| div 스크롤바를 왼쪽에 배치하는 방법은 무엇입니까? (0) | 2020.11.26 |
| pdf 파일 다운로드를위한 올바른 PHP 헤더 (0) | 2020.11.26 |
| CakePHP 3.0 설치 : 시스템에 intl 확장이 없습니다. (0) | 2020.11.26 |
| Java, Apache Kafka에서 주제의 메시지 수를 얻는 방법 (0) | 2020.11.26 |