[英]UILabel is nil- canot update text of UILabel
因此,我不得不承認,這是一個初學者的問題,但是我真的不知道該怎么做,並且花了我幾個小時才能解決。
所以:我的問題是,為什么我要更新其文本時UILabel為nil?
細節:
1:我有兩個視圖,檢測視圖是初始視圖,該視圖的工具欄中有一個UILabel和一個名為“設置”的工具欄按鈕項,第二個視圖是EnterCommandView,其中有一個UITextField和一個條形圖該視圖頂部工具欄中名為“保存”的按鈕項。
2:輸入字符串后,應用程序啟動完成后,單擊“設置”,然后將表單從第一個視圖切換到第二個視圖。 第二,我在UITextField中輸入一些字符串,然后單擊“保存”按鈕,第二個視圖被關閉,然后回到初始視圖,
3: 當我回到InitialView時,剛輸入的字符串應該出現在UILabel中,但是,正確的不是,這正是我的問題,然后我在更新UILabel的位置設置了一個斷點,本文結尾處的信息,表示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
以下是我設置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>
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.