I have a NSString property, self.textFromTextVC, in a Viewcontroller and it's value becomes null in the IBAction method.
- (IBAction)buttonPressed:(id)sender
{
NSLog(#"text before alarm is created: %#", self.textFromTextVC);
}
The methods below are in the same '.m' file and they keep the value of the NSString property.
-(void)setPropertyTextToReceivedText:(NSString *)text
{
self.textFromTextVC = text;
NSLog(#"text received from text VC: %#", self.textFromTextVC);
[self doesStringKeepValue]; //I call this method to check and see if the NSString value
//was retained
}
-(void)doesStringKeepValue
{
NSLog(#"keep value: %#", self.textFromTextVC); //NSString value the same from the above
//method
}
Below is how I have declared the NSString property:
#property (nonatomic, copy) NSString *textFromTextVC;
Basically, I'm setting the self.textFromTextVC before the IBAction method is called and that is why I'm confused. I'm really not sure what is going on. I have ARC selected.
I'm hoping that I'm just making a simple mistake...help?
Thanks,
Below is the method in another viewcontroller where I called setPropertyTextToReceivedText:
#implementation TextViewController
#synthesize typedText;
- (IBAction)doneButton:(id)sender {
[self.typedText resignFirstResponder];
AlarmViewController *receiver = [[AlarmViewController alloc]init];
[receiver setPropertyTextToReceivedText:self.typedText.text];
//[self showAlert];
}
What your problem is receiver is different object than your VC which is shown (present/pushed).
AlarmViewController *receiver = [[AlarmViewController alloc]init];
[receiver setPropertyTextToReceivedText:self.typedText.text];
Change this:
NSLog(#"text before alarm is created: %#", self.textFromTextVC);
To this:
NSLog(#"%#: text before alarm is created: %#", self, self.textFromTextVC);
And it will probably show you that you are indeed looking at two different object instances of the same class.
I think you forgot:
#synthesize textFromTextVC;
Related
I want to set the value of NSTextField.
//In .h Class//I have an
IBOutlet #property (assign) IBOutlet NSTextField *tId;
//In .m Class//
#synthesize tId;
In my XIB I have connected the IBOulet to the FilesOwner.
and in my .m class I am trying to set the value to the NSTextField in the method
- (id)initWithWindow:(NSWindow *)window
{enter code here
[tId setStringValue:#"1"];
return self;
}
But the value is not getting set.
Thanks in Advance!!!!!
OKs Use this
-(BOOL)isPartialStringValid:(NSString*)partialString newEditingString:(NSString**)newString errorDescription:(NSString**)error
{
if([partialString length] == 0) {
return YES;
}
NSScanner* scanner = [NSScanner scannerWithString:partialString];
if(!([scanner scanInt:0] && [scanner isAtEnd])) {
NSBeep();
return NO;
}
return YES;
}
And then set this formatter to your NSTextField:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[textField setFormatter:formatter];
I'm not sure if I've understood the question correctly. I'll address the problem "I try to set stringValue to NSTextField accessing it via IBOutlet, but value in text field doesn't change", not the problem "How to customize NSTextField, so it accepts only numeric values".
NSTextField value may not be set because
method initWithWindow: isn't called
IBOutlets aren't hooked up yet when you call -[NSTextField setStringValue:].
Usually, IBOutlets are nil in initializers and are hooked up later. I'd move the line
[tId setStringValue:#"1"];
to method - (void)awakeFromNib and see if helps. According to docs
When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set.
NSLog is returning the output 'Null" instead of a string that I would have expected. I suspect that this is a problem with private instance variables and such, but since I am not familiar with Object-oriented programming I cannot determine the cause.
//The viewDidLoad method in MainGameDisplay.m:
- (void)viewDidLoad
{
[super viewDidLoad];
Engine *engine = [[Engine alloc] init];
[engine setPlayerName: viewController];
}
The string is entered by a UITextField, the property being
//ViewController.h
#property (strong, nonatomic) IBOutlet UITextField *PlayerNameTextView;
The method works fine and returns the correct string if [engine setPlayerName: self] is placed into ViewController, but anywhere outside the location that *PlayerNameTextView is causes this problem.
//Engine.m
#implementation Engine
{
ViewController *firstPage;
}
NSString *Player;
-(void) setPlayerName: (ViewController *) name
{
Player = [[name PlayerNameTextView] text];
NSLog(#"%#", Player);
}
NSLog return type is void as you can see in it's documentation. There is no reason to expect any return value for a call to it, since it does not return anything.
Make sure that 'name' is properly initialized. Try putting an assert(name != nil) right before the NSLog. Or better yet, set a breakpoint at the NSLog and inspect the variables.
Another suggestion: Why not make the method -(void) setPlayerName:(NSString*)name? This is more straightforward than passing around pointers to view controllers, and would be easier to debug.
I've got NSMutableArray filled with custom class called "Audio"
It is with this properties:
#property(nonatomic,retain) NSString *artist;
#property(nonatomic,retain) NSString *title;
#property(nonatomic,retain) NSString *duration;
#property(nonatomic,retain) NSString *audio_id;
First my action is print them in UITableView. This is going without any problem.
But second action, showing “EXC_BAD_ACCESS”, when i want to access audio_id property, to pass it into other method as param:
Audio *audio = [musicList objectAtIndex:indexPath.row];
[self dosomething:audio.audio_id];
Here is: "EXC_BAD_ACCESS"
Please, help, anybody:)
maybe this will help you debug better:
Audio *audio = [musicList objectAtIndex:indexPath.row];
if(audio){
[self doSomething:audio.audio_id];
}
else if(musicList){ NSLog(#"musicList is not nil and has a count of %d.",[musicList count]); }
else{ NSLog(#"musicList is nil and audio is nil, something is getting released too soon."); }
I am trying to set the value of an NSTextField, but it's not working properly.
I have a button linked to an IBAction, and when I set it using self, it works fine:
#import <Foundation/Foundation.h>
#interface TestMessage : NSObject {
IBOutlet NSTextField *text;
}
- (IBAction) setMessage: (id) controller;
- (void) Message:(NSString *) myMessage;
#end
#import "TestMessage.h"
#implementation TestMessage
- (IBAction) setMessage: (id) controller {
// This works
[self Message:#"Hello"];
// but this doesn't
TestMessage * messageTest= [TestMessage new];
[messageTest Message:#"Hi"];
}
- (void) Message: (NSString *) myMessage {
[text setStringValue: myMessage];
NSLog(#"Message Was Called");
// This returns <NSTextField: 0x1001355b0> when called
// using self, but null when called the other way.
NSLog(#"%#", text);
}
#end
I've searched for a while, but still can't find the answer.
I guess it has something to do with the delegate, but I'm not sure.
Thanks in advance.
Are you sure message is called when you call it from anotherFuntion? If anotherFuntion is a method of another class, calling [self message:] won't work as you expected to...
I know this is an old post, but I have been fiddling with the same issue today.
You have to return string value in textfield:
[textField stringValue];
The code
TestMessage * messageTest = [TestMessage new];
is unusual, specifically new. I'm going to assume that new is just a class method does normal alloc/init equivalent to
TestMessage * messageTest = [[TestMessage alloc] init];
The main problem is that IBOutlet NSTextField *text will be initialized only if the class TestMessage is loaded with a Nib file. It would have to be named as the class of an object in Interface Builder, like so
and you would have to implement initWithCoder and encodeWithCoder something like this in order to extract your field value from the IB encoding:
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.text = [coder decodeObjectForKey:#"text"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:self.text forKey:#"text"];
}
Fundamentally, IBOutlet fields do not get wired up wherever you create an instance of that class. If they did, how would you express that field A should be wired to UI object A and field B should be wired to UI object B? The connection is established only in the context of loading a class from a Nib file.
I'm trying to simply add objects to a mutable array but they WILL NOT insert. I'm not getting errors or anything and I can't figure out whats going on.
In my main delegate file I split an array into 4 separate strings like so.
NSArray *split=[currentParsedCharacterData componentsSeparatedByString:#"|"];
NSLog([split objectAtIndex:3]);
NSString *date=[split objectAtIndex:0];
NSString *venue=[split objectAtIndex:1];
NSString *event=[split objectAtIndex:2];
NSString *city=[split objectAtIndex:3];
I've traced out the string values and they are definitely there.
Up next I try to add these string values to mutable arrays
[self.promoTabOptionEvents.dates addObject:date];
[self.promoTabOptionEvents.venues addObject:venue];
[self.promoTabOptionEvents.event addObject:event];
[self.promoTabOptionEvents.city addObject:city];
When I check the arrays in the debugger they are empty. What am I doing wrong?
promoTabOptionEvents class looks like this
import
#interface PromoTabOptionEvents : UIViewController {
NSString *event_headline;
NSMutableArray *dates;
NSMutableArray *venues;
NSMutableArray *event;
NSMutableArray *city;
}
#property(nonatomic,retain)NSString *event_headline;
#property(nonatomic,retain)NSMutableArray *dates;
#property(nonatomic,retain)NSMutableArray *venues;
#property(nonatomic,retain)NSMutableArray *event;
#property(nonatomic,retain)NSMutableArray *city;
-(void)applyLabels;
-(id)initWithTabBar;
#end
#import "PromoTabOptionEvents.h"
#implementation PromoTabOptionEvents
#synthesize event_headline;
#synthesize dates;
#synthesize venues;
#synthesize event;
#synthesize city;
-(id) initWithTabBar {
if ([self init]) {
//this is the label on the tab button itself
//self.title = #"Tab1";
//use whatever image you want and add it to your project
self.tabBarItem.image = [UIImage imageNamed:#"events.png"];
// set the long name shown in the navigation bar
self.navigationItem.title=#"Events";
CGRect bgframe;
bgframe.size.width=320; bgframe.size.height=460;
bgframe.origin.x=0; bgframe.origin.y=0;
UIImage* bgimage = [UIImage imageNamed:#"eventsbig.png"];
UIImageView *imagebgview = [[UIImageView alloc] initWithImage: bgimage];
imagebgview.frame=bgframe;
imagebgview.contentMode=UIViewContentModeScaleAspectFit;
self.view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:imagebgview];
}
return self;
}
Can you add the code where you initialize your NSMutableArray instances? I think you might have forgotten to initialise the arrays and your addObject calls are being swallowed up with no effect.
Are you instantiating the properties anywhere, and if so, have you debugged through to verify that is the case? Otherwise you may be sending messages to nil, which will have no effect. Alternatively, you may be doing the array creation after this call, which would make it look like they're not added.