UILabel无法更新UILabel的文本

时间:2015-04-11 23:26:31

标签: ios objective-c iphone uilabel

所以这是一个初学者的问题,我不得不承认,对你们来说,但我真的不知道自己应该做些什么,而且我花了几个小时尝试修复它。

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

详细说明:

1:我有两个视图,而DetectionView是初始视图,其中有一个名为"设置"的UILabel和一个条形按钮项。在此视图按钮的工具栏中,第二个视图是EnterCommandView,其中有一个UITextField和一个名为&#34的栏按钮项;保存"在此视图顶部的工具栏中。

2:输入字符串后,应用程序完成启动后,单击“设置”,然后我将第一个视图转换为第二个视图。第二,我在UITextField中输入一些字符串,然后点击" Save"按钮,第二个视图被关闭,然后我回到初始视图,

3:当我回到InitialView时,刚进入的字符串应该出现在UILabel中,但是,它没有,这正是我的问题,然后我在我所在的地方设置断点更新UILabel,我在这篇文章末尾有信息,说UILabel是零

代码:

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

以下是我设置breakPoint AT DetectionViewController.m 时所获得的 - &gt; - (void)commmandEntered:(NSString *)command {} - &gt; 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>      

1 个答案:

答案 0 :(得分:1)

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;
});
}