gpt4 book ai didi

ios委托(delegate)不触发

转载 作者:行者123 更新时间:2023-12-01 16:54:55 25 4
gpt4 key购买 nike

我尝试了一个简单的委托(delegate),但该方法不会触发。这是我的代码:
带有协议(protocol)的 View 和一个应该触发委托(delegate)的按钮:
View Controller .h

#import <UIKit/UIKit.h>
@protocol InitStackDelegate <NSObject>
@required
-(void)initstack:(NSInteger)amount;
@end

@interface ViewController : UIViewController{
IBOutlet UITextField *amountTextField;
__unsafe_unretained id<InitStackDelegate> delegate;
}

- (IBAction)init:(id)sender;

@property (nonatomic,assign)id delegate;

@end

View Controller .m
#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController
@synthesize delegate;
- (IBAction)init:(id)sender {
//send the delegate

[delegate initstack:[amountTextField.text intValue]];

}

AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,InitStackDelegate> {
}

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) ViewController *myView;
@end

AppDelegate.m
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window;
@synthesize myView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//..... push second controller into navigation stack
myView.delegate = self;
return YES;
}
...
#pragma mark Delegate Method
-(void)initstack:(NSInteger)amount{
NSInteger test;
}

@end

当我按下按钮时,我进入(IBAction)初始化,但委托(delegate)什么也不做。我在 AppDelegate.m 中设置了一个断点,但从未达到。有什么帮助吗?

谢谢
马里奥

最佳答案

在您的 App Delegate 中,当您设置 myView.delegate=self myView 实际上并没有指向任何东西!
您需要将 myView 设置为指向您的 ViewController。

didFinishLaunchingWithOptions 中使用它

myView = (ViewController *)self.window.rootViewController;
myView.delegate = self;

关于ios委托(delegate)不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com