programing

iOS - ViewController에서 앱 위임 메서드를 호출합니다.

codeshow 2023. 4. 12. 22:44
반응형

iOS - ViewController에서 앱 위임 메서드를 호출합니다.

여기서 하려고 하는 것은 (코드로 작성된) 버튼을 클릭하여 다른 뷰 컨트롤러를 호출하고 새로운 뷰 컨트롤러에서 기능을 실행하는 것입니다.

IB에서 비교적 쉽게 할 수 있다는 것을 알지만 그것은 선택사항이 아닙니다.

예를 들어, 2개의 뷰 컨트롤러에 집의 스플래시 스크린이 있는 것을 예로 들 수 있습니다.다른 뷰 컨트롤러에는 정해진 순서로 모든 방을 살펴볼 수 있는 집이 있습니다.시작 화면에는 각 룸에 대한 버튼이 있어 워크스루 중 원하는 위치로 이동할 수 있습니다.

대리자는 다음과 같이 액세스할 수 있습니다.

MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate];

MainClass를 응용 프로그램클래스의 이름으로 바꿉니다.

다른 뷰 컨트롤러의 속성이 있는 경우 다음과 같이 호출할 수 있습니다.

[appDelegate.viewController someMethod];

그리고 이 은 ' 좋을지'에서swift:

if let myDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
   myDelegate.someMethod()
}

꼭 필요한 것 같네요UINavigationController 디폴트랜덤?

해서 얻을 수 요.AppDelegate 내 의 장소

YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate];

앱 위임에서 IB 또는 코드를 통해 내비게이션 컨트롤러를 설정해야 합니다.

Overview' 하면, 'House Overview' 뷰컨트롤러는 이렇게 예요.AppDelegate didFinishLaunchingWithOptions

self.m_window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds] autorelease];
self.m_navigationController = [[[UINavigationController alloc]initWithRootViewController:homeViewController]autorelease];
[m_window addSubview:self.m_navigationController.view];

그 후 '방'마다 뷰 컨트롤러만 있으면 되고 버튼 클릭 이벤트가 발생하면 다음을 호출하면 됩니다.

YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate];
[blah.m_navigationController pushViewController:newRoomViewController animated:YES];

위의 코드를 테스트하지 않았으므로 구문 오류를 용서해 주십시오.하지만 유사 코드가 도움이 되었으면 합니다.

이렇게 하는 거예요.

[[[UIApplication sharedApplication] delegate] performSelector:@selector(nameofMethod)];

잊지 말고 Import해 주세요.

#import "AppDelegate.h"

다음 단계를 따르기만 하면 됩니다.

1. 클래스에서 앱 위임 개체를 원하는 위치에 앱 위임자를 가져옵니다.

#import "YourAppDelegate.h"

2. 클래스 내에서 앱 위임 오브젝트(기본적으로 싱글톤)의 인스턴스를 만듭니다.

YourAppDelegate *appDelegate=( YourAppDelegate* )[UIApplication sharedApplication].delegate;

3. 셀렉터를 사용하여 메서드를 호출합니다.

if([appDelegate respondsToSelector:@selector(yourMethod)]){

        [appDelegate yourMethod];
    }

또는 직접 경유하여

[appDelegate yourMethod];

재빠르게

let appdel : AppDelegate = UIApplication.shared.delegate as! AppDelegate

첫 번째 거 추천할게요.실행 및 실행.

NSObject <UIApplicationDelegate> * universalAppDelegate = 
    ( NSObject <UIApplicationDelegate> * ) [ [ UIApplication sharedApplication ] delegate ];

모든 곳에 AppDelegate.h를 포함할 필요가 없습니다.단순한 캐스트이기 때문에 클래스 이름 등에 신경 쓰지 않고 독립 컨트롤러를 개발하여 다른 곳에서 재사용할 수 있습니다.

즐거운 시간 되세요.

이미 많은 좋은 답변들이 추가되어 있습니다.대부분의 경우 나에게 맞는 것을 추가하고 싶지만.

#define kAppDelegate ((YourAppDelegate *)[[UIApplication sharedApplication] delegate]);

그게 다예요애플리케이션 전체에서 상수처럼 사용합니다.

예.

[kAppDelegate methodName];

AppDelegate.h를 대응하는 .pch 파일 또는 매크로 파일로 Import하는 것을 잊지 마십시오.

Xamarin(Xamarin.ios/Monotoouch)에서도 같은 것을 필요로 하는 사람이 있다면, 다음과 같이 하십시오.

var myDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;

(UIKit 사용 필요;)

Swift 3.0 이후 업데이트

//
// Step 1:- Create a method in AppDelegate.swift
//
func someMethodInAppDelegate() {

    print("someMethodInAppDelegate called")
}

컨트롤러에서 위 메서드를 호출하는 방법은 다음과 같습니다.

//
// Step 2:- Getting a reference to the AppDelegate & calling the require method...
//
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {

    appDelegate.someMethodInAppDelegate()
}

출력:

여기에 이미지 설명 입력

또한 WatchKit 확장 기능에 대한 액세스 권한이 Watch 컨트롤러에서 필요한 경우OS:

extDelegate = WKExtension.sharedExtension().delegate as WKExtensionDelegate?

추가할 수 있습니다.#define uAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]프로젝트 기간 중Prefix.pch파일링 후, 임의의 방법으로 호출합니다.AppDelegate어느 쪽이든UIViewController아래 코드와 함께.

[uAppDelegate showLoginView];

2020년 iOS13 xCode 11.3.Objective-C에서 유효한 것은 다음과 같습니다.

#import "AppDelegate.h"

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

나한텐 효과가 있어, 너도 마찬가지였으면 좋겠어!d

기술적으로 실현 가능하다고 해도 좋은 접근법은 아닙니다."스플래시 화면에는 각 룸의 버튼이 있어 워크스루 중 어느 포인트로든 이동할 수 있습니다."라고 말합니다.그러면 버튼의 tohc 이벤트를 통해 컨트롤러에 전화를 걸기 위해 applegate를 통과하시겠습니까?

이 접근방식은 Apple의 가이드라인을 따르지 않으며 많은 단점이 있습니다.

언급URL : https://stackoverflow.com/questions/5082738/ios-calling-app-delegate-method-from-viewcontroller

반응형