Reputation: 3
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?
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.
2: After I enter a String, After the app finish launching, I click Setting, then I segue form first View to second View. 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,
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
Code:
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
Below is what I got when I set a 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>
Upvotes: 0
Views: 156
Reputation: 78
There is nothing Wrong with UILabel, the problem, I guess, is the format of the "commnad" is not compatible with the text in 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;
});
}
Upvotes: 1