[英]UILabel is nil- canot update text of UILabel
So this a beginner's question, I have to admit, to you guys, but I really have no idea what I should do ,and it have taken me several hrs to try to fix it. 因此,我不得不承认,这是一个初学者的问题,但是我真的不知道该怎么做,并且花了我几个小时才能解决。
So: My question is that why the UILabel is nil when I want to update its text? 所以:我的问题是,为什么我要更新其文本时UILabel为nil?
Details: 细节:
1:I have two Views and DetectionView is the initial View where there is a UILabel and a bar button item named "Setting" in the toolbat at the button of this view, and second View is the EnterCommandView where there is a UITextField and a bar button item named "save" in the toolbar at the top of this view. 1:我有两个视图,检测视图是初始视图,该视图的工具栏中有一个UILabel和一个名为“设置”的工具栏按钮项,第二个视图是EnterCommandView,其中有一个UITextField和一个条形图该视图顶部工具栏中名为“保存”的按钮项。
2: After I enter a String, After the app finish launching, I click Setting, then I segue form first View to second View. 2:输入字符串后,应用程序启动完成后,单击“设置”,然后将表单从第一个视图切换到第二个视图。 In second, I enter some string in the UITextField and then click "Save" button, the second View is dismissed, and then I go back to the Initial View,
第二,我在UITextField中输入一些字符串,然后单击“保存”按钮,第二个视图被关闭,然后回到初始视图,
3: When I go back to the InitialView, the String just entered should appear in the UILabel, but , right it does not, which is exactly my problem, And then I set a breakpoint at the place where I update the UILabel, I have the info at the end of this post, saying UILabel is nil 3: 当我回到InitialView时,刚输入的字符串应该出现在UILabel中,但是,正确的不是,这正是我的问题,然后我在更新UILabel的位置设置了一个断点,本文结尾处的信息,表示UILabel为nil
Code: 码:
EnterCommnadViewController.h 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 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 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 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
Below is what I got when I set a breakPoint AT DetectionViewController.m --> -(void)commmandEntered:(NSString*)command{} --> showReceivedCommand.text = command; 以下是我设置BreakPoint AT DetectionViewController.m时得到的结果-> -(void) commmandEntered :(NSString *)command {} -> showReceivedCommand.text = command;
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>
There is nothing Wrong with UILabel, the problem, I guess, is the format of the "commnad" is not compatible with the text in UILabel. 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;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.