Ios alloc和Init的失败
所以,也许这是一个初学者的错误,对你们来说非常容易,但我真的不知道如何解决它,非常感谢你们的建议: 现在: 1:我必须查看控制器:输入CommandViewController和DetectionViewController 2:我在EnterCommandViewController中编写了委托协议,并将DetectionViewController设置为其委托 3:关于代理:我在EnterCommandView中有一个InputExtField,在该视图的顶部工具栏上有一个“保存”栏按钮项。单击保存后,当前视图将被取消并返回到DetectionView,并在DetectionView中显示刚刚在UILabel中输入的NSString 最后,我的问题是,为什么在我分配并初始化EnterCommandViewController实例(即enterCVS)后,该实例仍然为零,如我文章末尾所示。 代码: EnterCommandViewController.hIos alloc和Init的失败,ios,objective-c,delegates,Ios,Objective C,Delegates,所以,也许这是一个初学者的错误,对你们来说非常容易,但我真的不知道如何解决它,非常感谢你们的建议: 现在: 1:我必须查看控制器:输入CommandViewController和DetectionViewController 2:我在EnterCommandViewController中编写了委托协议,并将DetectionViewController设置为其委托 3:关于代理:我在EnterCommandView中有一个InputExtField,在该视图的顶部工具栏上有一个“保存”栏按钮项。
#import <UIKit/UIKit.h>
#import "RscMgr.h"
@protocol EnterCommandDelegate <NSObject>
@optional
-(void) commandEntered:(NSString*)command;
@end
@interface EnterCommandViewController : UIViewController <RscMgrDelegate,EnterCommandDelegate>
{
RscMgr* rscMgr;
IBOutlet UITextField *inputTextField;
// DetectionViewController* detectionViewController;
// __unsafe_unretained id<EnterCommandDelegate> delegate;
}
-(void)sendMessage:(NSString*)message;
-(id)initWithDelegate:(id)delegateToBe;
- (IBAction)cancelPressed;
- (IBAction)savePressed;
@property (nonatomic,weak) id<EnterCommandDelegate> delegate; //assign replaced
@end
#import <UIKit/UIKit.h>
#import "EnterCommandViewController.h"
@interface DetectionViewController : UIViewController <EnterCommandDelegate>{
}
- (IBAction)showSettings:(UIBarButtonItem *)sender;
@property (nonatomic, strong) EnterCommandViewController* enterCVC;
@property (nonatomic, strong) IBOutlet UILabel *showReceivedCommand;
@end
试着把它改成
enterCVC = [[EnterCommandViewController alloc] initWithDelegate:self];
什么是RscMgr类?另外,哪个视图控制器是初始视图控制器,以及如何从第一个视图控制器转到第二个视图控制器?RscMgr.h是我正在使用的另一个第三方库。DetectionView是初始视图,如何从DetectionViewController转到EnterCommandViewController?你在故事板上做这些吗?您正在使用segue吗?我在DetectionView底部的工具栏中有一个条形按钮项(名为“设置”),它将segue(show)进入CommandView您不应该使用alloc init创建控制器的实例。segue创建实例。您应该实现prepareForSegue,在这里您可以获得对目标视图控制器的引用,并将自己设置为那里的代理。
#import <Foundation/Foundation.h>
#import "DetectionViewController.h"
#import "EnterCommandViewController.h"
@implementation DetectionViewController
@synthesize showReceivedCommand;
@synthesize enterCVC;
- (IBAction)showSettings:(UIBarButtonItem *)sender {
}
-(void) viewDidLoad{
[super viewDidLoad];
if(showReceivedCommand){
showReceivedCommand.text=@"Initial text";
NSLog(@"UILAbel in ViewDidload is not nil");
}else {
NSLog(@"UILAbel in viewDidload is nil");
}
enterCVC = [[EnterCommandViewController alloc] init];
if(enterCVC.delegate) NSLog(@"X nil");
[enterCVC setDelegate:self];
}
#pragma mark - EnterCommandDelegate function(s)
-(void)commandEntered:(NSString *)command{
dispatch_async(dispatch_get_main_queue(), ^{
if(showReceivedCommand){
NSLog(@"UILabel is not nil");
}else{NSLog(@"UILabel is nil");}
showReceivedCommand = [[UILabel alloc] init];
NSLog(@"command received: %@",command);
showReceivedCommand.text = command;
[showReceivedCommand setNeedsDisplay];
NSLog(@"text in showReceivedCommand is %@",showReceivedCommand.text);
});
}
@end
self DetectionViewController * 0x15c50e850 0x000000015c50e850
UIViewController UIViewController
showReceivedCommand UILabel * 0x15c510650 0x000000015c510650
enterCVC EnterCommandViewController * 0x15c611360 0x000000015c611360
showReceivedCommand UILabel * 0x15c510650 0x000000015c510650
enterCVC EnterCommandViewController * 0x15c611360 0x000000015c611360
UIViewController UIViewController
rscMgr RscMgr * nil 0x0000000000000000
inputTextField UITextField * nil 0x0000000000000000
connected BOOL NO false
delegate id 0x0 0x0000000000000000
enterCVC = [[EnterCommandViewController alloc] init]
enterCVC = [[EnterCommandViewController alloc] initWithDelegate:self];