UITextView returns number instead of text - objective-c

stackoverflow, google and coffee have failed to find the answer, however......
specialViewController.h has...
#interface SpecialViewController : UIViewController {
UITextView *messageBody;
}
#property (nonatomic, retain) UITextView *messageBody;
specialViewController.m has....
#implementation SpecialViewController
#synthesize messageBody;
plus....
-(IBAction)executeSpecial:(id)sender{
[self.messageBody resignFirstResponder];
NSString *message = messageBody.text;
NSLog(#"returned text is %#", message);
}
shows "returned text is 148082" or some random number dependent on the text entered. I just cannot, for the life of me, find out why the text I am entering is not being shown.

Generally, problems like this are from other part of the code that you may not expect. I noticed that you don't have your UITextView marked as an IBOutlet. Are you adding it programmatically? If so, can you give us the code for that.

Sorry Gents, problem was decaf coffee! During the course of my coding, I repurposed the variable and this ultimately conflicted. It turns out that I was getting exactly what I asked for. I always find it strange that my applications never do what I want, only what I tell them to......

Related

Objective-C: using methods from other files

I have looked at all the other people who had this error pop up, and I know it is a pathing error, and yes I have tried to quit and reboot it, I checked the paths were using up-to-date files, and most of what people suggested with no success. I am trying to get methods from TapViewController to run in JumpController. I simplified the method I want to be called to make it easier to find the issue, but still am having trouble. Here is the relevant code I have so far:
TapViewController.h
-(void)hello;
TapViewController.m
-(void)hello {
NSLog(#"Hello");
}
JumpController.h
#import <UIKit/UIKit.h>
#import 'TapViewController.h'
#property (strong, nonatomic) TapViewController *TapView;
#property (strong, nonatomic) JumpController *JumpControl;
JumpController.m
-(void)viewDidLoad {
self.JumpControl = (TapViewController *)[UIApplication sharedApplication].delegate;
self.TapView = [[TapViewController alloc] init];
[self.JumpControl.TapView hello];
}
I grabbed most of this code from what others have said to do, so I don't really know if some of it is irrelevant or if all will help in the situation. Basically, the app crashes when it loads stating [AppDelegate TapView]: unrecognized selector sent to instance.... Let me know if I am doing anything wrong or if I left out relevant code!
UPDATE: Using what others have said and from my own personal changes, It seems like the problem is not only with self.JumpControl = (TapViewController *)[UIApplication sharedApplication].delegate but also with self.JumpControl as a whole. Because I created the TapViewController *TapView in the .h file there is no reason to use self.JumpControl which caused problems on the order of views that showed up. I will mark almas as correct, but I wanted to clarify what more needed to be done.
Your problem is here: "self.JumpControl = (TapViewController *)[UIApplication sharedApplication].delegate;". You are trying to cast your AppDelegate to TapViewController, thats why it crashes. AppDelegate is not a view controller. Your error message [AppDelegate TapView]: unrecognized selector sent to instance clearly states that AppDelegate doesn't recognize the method "TapView", it is because app delegate is not an instance of "TapViewController".

How to declare a variable from user input?

I am fairly new to programming and am working with Objective-C in Xcode 5.
I'm presently making an OSX application in Xcode that uses Cramer's Rule (this matrix math method to calculate the intersecting point of three lines).
I really need some help with this one concept- I need to be able to take the user's input from multiple text boxes (assign them all a variable), put them through cramer's rule, and feed the answer out through a label.
I've made the storyboard and assigned one of the 12 text boxes (to test it) as an outlet and the label as an outlet and a button as an action, and tried a few different ways to just take the user input and (unaltered) feed it back out through the label so I know what I'm working with before I get into the math, and it's been unsuccessful. Having major syntax problems.
I have attached my code below:
//
// NewClass.h
// Cramer's Rule
#import <Foundation/Foundation.h>
#interface NewClass : NSViewController <NSTextFieldDelegate> {
IBOutlet NSTextField *box_a;
IBOutlet NSTextField *coord;
NSString *string;
}
#property (nonatomic, retain) IBOutlet NSTextField *box_a;
#property (nonatomic, retain) IBOutlet NSTextField *coord;
- (IBAction)calculate:(id)sender;
#end
AND
//
// NewClass.m
// Cramer's Rule
#import "NewClass.h"
#implementation NewClass
#synthesize box_a;
#synthesize coord;
- (IBAction)calculate:(id)sender {
NSTextField * input=box_a;
coord =input;
}
#end
As far as I know, I have the most up to date version of Xcode, and there is no option for creating a storyboard for an OSX project. Storyboards are for iOS projects. And that would explain the reason why you're unable to hook any thing up from the storyboard to your code.
This isn't to say that a storyboard can't be put in an OSX project--it can't. But it can't be selected from the Cocoa section of new files to create--only the Cocoa Touch section, which is iOS stuff--not OSX.
You have to use NSTextFieldDelegate, it have callback methods like in iOS:
- (void)textDidBeginEditing:(NSNotification *)notification;
- (void)textDidEndEditing:(NSNotification *)notification;
- (void)textDidChange:(NSNotification *)notification;
- (BOOL)acceptsFirstResponder;
For example:
- (void)textDidChange:(NSNotification *)notification{
if ([notification object]== box_a)
{
// ...
}else if ([notification object]== box_b)
{
// ...
}
}
Your problem is more fundamental than syntactical, you need to go and study up on what various things are and how they behave, this includes: variables, properties, objects and object references.
To briefly introduce why you're going wrong: Think of an object as a building. What is "in" the building may change over time, but the address of the building (usually!) does not. An address refers you to a building, and that is what an object reference does.
A variable is a box which holds a value of some type, that value can change over time, but the box does not.
When you declare:
NSTextField *input;
You are requesting that a variable be created for you which can hold references to objects - it does not hold an object anymore than address is a building, it just tells you where to find an object.
When you then assign a value to your variable:
NSTextField *input = box_a;
You are requesting the the value in box_a be copied and placed (stored) in input. That value is an object reference, it is not an object. Whatever object was referenced by box_a is not altered in anyway by this statement - what is in the house doesn't change, you just write the house's address down somewhere else.
When you then do:
coord = input;
you are doing the same thing - copying addresses. No objects are altered. The objects you are referring to are of type NSTextField, they have a visual representation on the screen, copying their addresses doesn't alter that visual representation anymore than copying the address of a building changes what is in the building.
When it comes to properties your code suggests a confusion between a property, which is a piece of code which does something, and its backing variable, a variable which that piece of code operates on.
Understanding these concepts is vital. You need to go an study up some more on programming.
HTH

NSTextField not responding

I created an NSTextField in my code:
.h :
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSTextField *numberOfConnectionsTextField;
}
#property (nonatomic, retain) NSTextField *numberOfConnectionsTextField;
.m :
#synthesize numberOfConnectionsTextField;
I change the value of the field here:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[numberOfConnectionsTextField setStringValue:#"0"];
}
And the value successfully changes. However, when I try to the change the value here:
- (void)connectionNumber:(NSString *)number {
[numberOfConnectionsTextField setStringValue:number];
NSRunAlertPanel(#"", number, #"", #"", #"");
}
The NSTextField didn't change. BUT, the NSRunAlertPanel did trigger with the 'number' string correctly.
Any ideas? I can't seem to find what I did wrong...
Your code looks quite good. There is no error, the first reason is always of not connecting the outlet to the object but as you are able to change in applicationDidFinishLaunching:, so that is not the issue here.
But You did not mentioned about "-(void)connectionNumber:(NSString *)" is this in the same class? or you have created same iboutlet in another class and missed something?
Or you manually released the outlet numberOfConnectionsTextField ? Please check if it is not null? like
if(numberOfConnectionsTextField){
NSLog(#"Still exist")
}
Another reason might be, from which method are you calling "-(void)connectionNumber:(NSString *)", many a times it is seen that people call set/get of outlet from init.
Please comment if you are doing every thing correct...
It's probably safer to use self.numberOfConnectionsTextField then the variable name numberOfConnectionsTextField alone. This kind of error happened to me when I first learn objective c. Some of the IBOutLet gone nil after a period of time.
Try to use self.variablename for strong , synthesize variable

Why can't I access variables when I subclass a customised UIViewController?

I am new to iOS dev and apologies if the answer is obvious...but it isn't to me.
I have an APP with a Navigation controller at its root.
I have many very similar looking areas of the app to be created.
These are each to be UItableviewcontroller which has had a fair bit of customising done to allow buttons and other controls beside the tableview which has been reduced in size to allow for controls beside and below it.
The buttons, text, background etc etc and the data that gets loaded must all be individual to the particular are of the APP.
I created a UItableviewcontroller subclass by simply adding a new file subclass in Xcode.
I created my interface in the xib...created all the methods to drive what I need to in it.
Looks great...all seems fine. If I use it alone...works well.
Problem: I can't figure out how to subclass my custom sub-classed UITVController!
None of its properties are available from inside the new sub-class.
I clearly don't understand how things work here.
I have tried adding a new file > UIViewcontroller sub-class and changing the superclass to my custom superclass...to no avail. No properties accessible.
I have dug and dug and become more confused than anything else.
Is someone kind enough to help me get it right. Frustration is building.
Thanks
Keispe
EDIT:
Whoa found the problem. I have had Xcode open for many many days with several projects open.
It had totally weirded out!
In fact jrturton and eugene...I did know what I was doing (I thought I was going crazy...done this before in my app and suddenly no worky) Xcode had totally lost it's brains!
Anyone seen Xcode do that before??? using 4.1
Bloody hell that wasted a heap of valuable time including yours.
Thanks fellas
When you subclass anything, you can access your parent's class properties by addressing self via dot syntax
#interface BaseClass : NSObject {
#public
NSString *baseclassString;
}
#property (nonatomic, copy) NSString *name;
#end
.h
#interface HigherClass : BaseClass
#end
.m
#implementation HigherClass
- (id)init {
self = [super init];
self.name = #"Hola";
self->baseclassString = #"Hola";
return self;
}
- (void)viewDidLoad {
NSLog(#"name: %#", self.name);
}
#end
This all isn't 100% memory clean but you've gotta get a hang of what is happening here and adjust it properly to your application.

Using Scanf with ObjC and iPhone

trying to make an iPhone app and going through the tutorials and books recommended by those before me :) I'm trying to find information on the scanf/storing user input data from a text field into a variable which I can use later in my program. The text field is actually a number field, so I am trying to save the integers they input and not the text since there won't be any in my case. Am I on the wrong path here? Any help would be greatly appreciated.
what about if i am essentially trying
to save the input that is a number to
begin with
You want NSNumberFormatter. The data formatters ( Apple Guide) handle conversions to and from strings as well as formatting for output.
I think rather than scanf, you're just looking to get the value from the text field as an NSString pointer.
If you use a UITextField in your interface, you can connect the UITextField to a member variable in your class by declaring the variable as an IBOutlet and connecting it in Interface Builder.
You can then access the text value as an NSString pointer by using [UITextField variable name].text.
There are many useful functions to work with NSStrings or convert the string to other datatypes such as integers.
Hope this helps!
Here's an example of how to get an integer out of a text field.
In your .h file:
#include <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
UITextField *myTextField;
}
#property (nonatomic, retain) IBOutlet UITextField *myTextField;
- (IBAction)buttonPressed1:(id)sender;
#end
In your .m file:
#include "MyViewController.h"
#implementation MyViewController
#synthesize myTextField;
- (IBAction)buttonPressed1:(id)sender {
NSString *textInMyTextField = myTextField.text;
// textInMyTextField now contains the text in myTextField.
NSInteger *numberInMyTextField = [textInMyTextField integerValue];
// numberInMyTextField now contains an NSInteger based on the contents of myTextField
// Do some stuff with numberInMyTextField...
}
- (void)dealloc {
// Because we are retaining myTextField we need to make sure we release it when we're done with it.
[myTextField release];
[super dealloc];
}
#end
In interface builder, connect the myTextField outlet of your view controller to the text field you want to get the value from. Connect the buttonPressed1 action to the button.
Hope this helps!