ios - UILabel 为 nil - 无法更新 UILabel 的文本

标签 ios objective-c iphone uilabel

所以我不得不承认,这是一个初学者的问题,但我真的不知道我应该做什么,我花了几个小时来尝试解决它。​​

所以:我的问题是,当我想更新其文本时,为什么 UILabel 为零?

详细信息:

1:我有两个 View ,DetectionView 是初始 View ,其中有一个 UILabel 和一个名为“Setting”的条形按钮项目,在该 View 按钮的工具栏中,第二个 View 是 EnterCommandView,其中有一个 UITextField以及此 View 顶部工具栏中名为“保存”的栏按钮项。

2:输入字符串后,应用程序启动完成后,单击“设置”,然后从第一个 View 转到第二个 View 。其次,我在 UITextField 中输入一些字符串,然后单击“保存”按钮,第二个 View 被关闭,然后我返回到初始 View ,

3: 回到InitialView的时候,刚才输入的String应该会出现在UILabel中,但是,对不对,正是我的问题,然后我在我的地方打了个断点更新 UILabel,我在这篇文章的末尾有信息,说 UILabel 是 nil

代码:

进入CommnadViewController.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;
});
}

关于ios - UILabel 为 nil - 无法更新 UILabel 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584226/

相关文章:

iphone - AsyncSocket 不调用委托(delegate)

iphone - 如何在不允许旋转其界面的 Controller 中旋转 View ?

iphone - 如何在 iOS 的 MKAnnotation 中添加更多细节

iphone - 我将如何扭转这个等式?

iphone - UIActivityViewController 背景颜色

iphone - 通过运行未签名的可执行文件检测越狱的 iOS 设备

ios - Firemonkey + iOS : How to activate In-App Purchase?

ios - ScrollView 不能从上到下平滑滚动

ios - 苹果手机SDK : how to distinguish between touch and drag

ios - 我需要制作一个可以被多个 View Controller 访问的数组实例