UILabel 为 nil - 无法更新 UILabel 的文本
UILabel is nil- canot update text of UILabel
所以这是一个初学者的问题,我不得不向你们承认,但我真的不知道我应该做什么,我花了几个小时来尝试解决它。
所以:我的问题是为什么当我想更新它的文本时 UILabel 为 nil?
详情:
1:我有两个视图,DetectionView 是初始视图,在该视图按钮的工具栏中有一个 UILabel 和一个名为 "Setting" 的条形按钮项,第二个视图是 EnterCommandView,其中此视图顶部的工具栏中有一个 UITextField 和一个名为 "save" 的条形按钮项。
2:输入字符串后,应用程序启动完成后,单击“设置”,然后从第一个视图转到第二个视图。其次,我在 UITextField 中输入一些字符串,然后单击 "Save" 按钮,第二个视图被关闭,然后我返回初始视图,
3: 当我回到InitialView的时候,刚刚输入的String应该出现在UILabel中,但是,对不对,这正是我的问题,然后我设置了一个断点我更新 UILabel 的地方,我在这个 post 的末尾有信息,说 UILabel 是 nil
代码:
EnterCommnadViewController.h
#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;
}
-(void)sendMessage:(NSString*)message;
-(id)initWithDelegate:(id)delegateToBe;
- (IBAction)cancelPressed;
- (IBAction)savePressed;
@property (nonatomic,weak) id<EnterCommandDelegate> delegate;
@end
EnterCommandViewController.m
#import "EnterCommandViewController.h"
#import "DetectionViewController.h"
@interface EnterCommandViewController () <UITextFieldDelegate>
{
@private
BOOL connected;
}
@end
@implementation EnterCommandViewController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
[inputTextField becomeFirstResponder];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
inputTextField.delegate = self;
}
-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
inputTextField.delegate = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)cancelPressed {
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (IBAction)savePressed {
if([[[UIDevice currentDevice]systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending){
NSLog(@"SYStem version > 7.0");
}
if(delegate&&[delegate respondsToSelector:@selector(commandEntered:)]){
[delegate commandEntered:inputTextField.text];
}
[self dismissViewControllerAnimated:YES completion:nil]; //commened: ^{}
}
@end
DetectionViewController.h
#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
DetectionViewController.m
#import <Foundation/Foundation.h>
#import "DetectionViewController.h"
@implementation DetectionViewController
@synthesize showReceivedCommand;
@synthesize enterCVC;
- (IBAction)showSettings:(UIBarButtonItem *)sender {
}
-(void) viewDidLoad{
[super viewDidLoad];
}
#pragma mark - EnterCommandDelegate function(s)
-(void)commandEntered:(NSString *)command{
dispatch_async(dispatch_get_main_queue(), ^{
showReceivedCommand.text = command;
});
}
#pragma mark -sugue
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
enterCVC = (EnterCommandViewController*)segue.destinationViewController;
[enterCVC setDelegate:self];
}
@end
下面是我在 DetectionViewController.m 设置断点时得到的结果 --> -(void)commmandEntered:(NSString*)command {} --> showReceivedCommand.text = 命令;
self DetectionViewController * 0x1465132a0 0x00000001465132a0
command NSString * @"testStringJustEntered" 0x000000017424ada0
showReceivedCommand UILabel * 0x1465149d0 0x00000001465149d0
UIView UIView
UIResponder UIResponder
_layer CALayer * 0x17409ae50 0x000000017409ae50
_gestureInfo id 0x0 0x0000000000000000
_gestureRecognizers NSMutableArray * nil 0x0000000000000000
_subviewCache NSArray * @"0 objects" 0x00000001740033b0
_charge float 0 0
_tag NSInteger 0 0
_viewDelegate UIViewController * nil 0x0000000000000000
_backgroundColorSystemColorName NSString * nil 0x0000000000000000
_countOfMotionEffectsInSubtree NSUInteger 0 0
_viewFlags <anonymous struct>
UILabel 没有任何问题,我猜问题是 "commnad" 的格式与 UILabel 中的文本不兼容。
Try follow code for the delegate function:
-(void)commandEntered:(NSString *)command{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* str1=@"";
NSString* str=[str1 stringByAppendingString:command];
showReceivedCommand.text = str;
});
}
所以这是一个初学者的问题,我不得不向你们承认,但我真的不知道我应该做什么,我花了几个小时来尝试解决它。
所以:我的问题是为什么当我想更新它的文本时 UILabel 为 nil?
详情:
1:我有两个视图,DetectionView 是初始视图,在该视图按钮的工具栏中有一个 UILabel 和一个名为 "Setting" 的条形按钮项,第二个视图是 EnterCommandView,其中此视图顶部的工具栏中有一个 UITextField 和一个名为 "save" 的条形按钮项。
2:输入字符串后,应用程序启动完成后,单击“设置”,然后从第一个视图转到第二个视图。其次,我在 UITextField 中输入一些字符串,然后单击 "Save" 按钮,第二个视图被关闭,然后我返回初始视图,
3: 当我回到InitialView的时候,刚刚输入的String应该出现在UILabel中,但是,对不对,这正是我的问题,然后我设置了一个断点我更新 UILabel 的地方,我在这个 post 的末尾有信息,说 UILabel 是 nil
代码:
EnterCommnadViewController.h
#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;
}
-(void)sendMessage:(NSString*)message;
-(id)initWithDelegate:(id)delegateToBe;
- (IBAction)cancelPressed;
- (IBAction)savePressed;
@property (nonatomic,weak) id<EnterCommandDelegate> delegate;
@end
EnterCommandViewController.m
#import "EnterCommandViewController.h"
#import "DetectionViewController.h"
@interface EnterCommandViewController () <UITextFieldDelegate>
{
@private
BOOL connected;
}
@end
@implementation EnterCommandViewController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
[inputTextField becomeFirstResponder];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
inputTextField.delegate = self;
}
-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
inputTextField.delegate = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)cancelPressed {
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (IBAction)savePressed {
if([[[UIDevice currentDevice]systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending){
NSLog(@"SYStem version > 7.0");
}
if(delegate&&[delegate respondsToSelector:@selector(commandEntered:)]){
[delegate commandEntered:inputTextField.text];
}
[self dismissViewControllerAnimated:YES completion:nil]; //commened: ^{}
}
@end
DetectionViewController.h
#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
DetectionViewController.m
#import <Foundation/Foundation.h>
#import "DetectionViewController.h"
@implementation DetectionViewController
@synthesize showReceivedCommand;
@synthesize enterCVC;
- (IBAction)showSettings:(UIBarButtonItem *)sender {
}
-(void) viewDidLoad{
[super viewDidLoad];
}
#pragma mark - EnterCommandDelegate function(s)
-(void)commandEntered:(NSString *)command{
dispatch_async(dispatch_get_main_queue(), ^{
showReceivedCommand.text = command;
});
}
#pragma mark -sugue
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
enterCVC = (EnterCommandViewController*)segue.destinationViewController;
[enterCVC setDelegate:self];
}
@end
下面是我在 DetectionViewController.m 设置断点时得到的结果 --> -(void)commmandEntered:(NSString*)command {} --> showReceivedCommand.text = 命令;
self DetectionViewController * 0x1465132a0 0x00000001465132a0
command NSString * @"testStringJustEntered" 0x000000017424ada0
showReceivedCommand UILabel * 0x1465149d0 0x00000001465149d0
UIView UIView
UIResponder UIResponder
_layer CALayer * 0x17409ae50 0x000000017409ae50
_gestureInfo id 0x0 0x0000000000000000
_gestureRecognizers NSMutableArray * nil 0x0000000000000000
_subviewCache NSArray * @"0 objects" 0x00000001740033b0
_charge float 0 0
_tag NSInteger 0 0
_viewDelegate UIViewController * nil 0x0000000000000000
_backgroundColorSystemColorName NSString * nil 0x0000000000000000
_countOfMotionEffectsInSubtree NSUInteger 0 0
_viewFlags <anonymous struct>
UILabel 没有任何问题,我猜问题是 "commnad" 的格式与 UILabel 中的文本不兼容。
Try follow code for the delegate function:
-(void)commandEntered:(NSString *)command{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* str1=@"";
NSString* str=[str1 stringByAppendingString:command];
showReceivedCommand.text = str;
});
}