NSMutableString append string - nsmutablestring

Beginnersquestion:
Have three buttons representing letters a, e and i.
When the buttons are pressed, corresponding letters should show up in a label.
So when each button is pressed once, the label would say "aei".
Attached my code below. When pressing the buttons three times now, the label
only shows the last pressed button letter.
What am i doing wrong?
Thank you for your help!
#import "SecondViewController.h"
NSMutableString *stringLabel;
#interface SecondViewController ()
#end
#implementation SecondViewController
-(IBAction)type_a:(id)sender;{
[stringLabel appendString: #"a"];
NSLog(#"stringLabel is set to: %#", stringLabel);
label.text = stringLabel;
}
-(IBAction)type_e:(id)sender;{
[stringLabel appendString: #"e"];
NSLog(#"stringLabel is set to: %#", stringLabel);
label.text = stringLabel;
}
-(IBAction)type_i:(id)sender;{
[stringLabel appendString: #"i"];
NSLog(#"stringLabel is set to: %#", stringLabel);
label.text = stringLabel;
}

I don't know where you are initialising your NSMutableString but maybe you are missing this:
stringLabel = [[NSMutableString alloc] init];
Put that code in viewDidLoad and it should works because your code is right.
Also, where are you setting the connection between the label and your viewController? I can't see the
#property (weak, nonatomic) IBOutlet UILabel *label;
Check that too

In your post each time You are assigning value to label using
label.text = stringValue
assigns the new value by erasing the old value
where as you have to concatenate the old string with new stringValue..
store previous value of Label in a NSMutableString like
NSMutableString *previosValue = label.text;
now concatenate the buttonValue to previousValue and add to label

Related

objective c and xcode assigning value to label coming from a NSInteger type variable

It's me again, I've been struggling with this for the past hour and a half and can't seem to find a good way of implementing this. I'm basically trying to display results on a label on clicking of a button. (Just starting out with xcode so I'm not sure if that's the right term for that action). Anyway, here's my code and the method on my controller: I have
#interface Match : NSObject{
}
#property NSInteger *Id;
#property NSString *fighter1, *fighter2;
- (id) initWithWCFId:(NSInteger)matchId bracketId:(NSInteger)bracketId;
#end
#implementation Match
- (id) initWithWCFId:(NSInteger)matchId bracketId:(NSInteger)bracketId{
self = [self init];
if(self){
self.Id = &(matchId);
self.fighter1 = #"Person 1";
self.fighter2 = #"Person 2";
}
return self;
}
#end
--- controller ---
#interface ViewController : UIViewController{
/*IBOutlet UITextField *txtFieldBracketId;
IBOutlet UITextField *txtFieldMatchId;*/
}
#property (weak, nonatomic) IBOutlet UITextField *txtFieldBracketId;
#property (weak, nonatomic) IBOutlet UITextField *txtFieldMatchId;
- (IBAction)btnSubmit:(id)sender;
#end
--- implementation
- (IBAction)btnSubmit:(id)sender {
#autoreleasepool {
Match *match = [[Match alloc]initWithWCFId:[_txtFieldMatchId.text integerValue] bracketId:[_txtFieldBracketId.text integerValue]];
self.lblMatchId.text = [[NSString alloc] initWithString:[NSNumber numberWithInt:match.Id]];
self.lblFighter1.text = [[NSString alloc] initWithString:match.fighter1];
self.lblFighter2.text = [[NSString alloc] initWithString:match.fighter2];
}
}
I basically have two text boxes.
Now when I click the button it'll get the value for those text boxes and then displays the data it got based off of those inputs. It'll then display the three following data:
Id, Fighter1 and Fighter2.
So what's happening is, when I click the button, the whole thing stops and gives me this error:
NSInvalidArgumentException', reason: '-[__NSCFNumber length]:
unrecognized selector sent to instance 0x74656e0'
* First throw call stack: (0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0xae4841 0x2891 0x10e1705 0x18920 0x188b8 0xd9671 0xd9bcf
0xd8d38 0x4833f 0x48552 0x263aa 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5
0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c
0x23dd 0x2305) libc++abi.dylib: terminate called throwing an exception
Now I'm not sure if 1. The way I designed my class is correct, using "NSInteger" for the property id. or
2. Assigning the Id integer to string (edit box) is wrong.
Two things:
The property should not be pointer type, so it should be #property NSInteger Id; and in init it should be just self.Id = matchId;
Make it to string by using [NSString stringWithFormat:#"%d", match.Id]
In addition to the issues with your Id property, the crash is coming from this:
self.lblMatchId.text = [[NSString alloc] initWithString:[NSNumber numberWithInt:match.Id]];
You are trying to pass an NSNumber object as the argument to the initWithString: method. But this method expects an NSString value, not an NSNumber.
Update the three lines to:
self.lblMatchId.text = [[NSString alloc] initWithFormat:#"%d", match.Id];
self.lblFighter1.text = match.fighter1;
self.lblFighter2.text = match.fighter2;
I'm assuming match.fighter1 and match.fighter2 are NSString properties.

Label not updating when passing string between tabs in UITabViewController

I am a beginner of iOS and I am writing some practice code in which I am trying to pass a string from one tab to another in a UITabViewController and using a label to display it in the next tab. Now the code where I am passing the message is:
-(IBAction)sendMessage:(id)sender
{
MessageRecepientViewController * contoller = [self.tabBarController.viewControllers objectAtIndex:1];
[contoller passString:_textField.text];
self.tabBarController.selectedIndex = 1;
}
And I am receiving the text here (in the second view controller) as:
-(void) passString:(NSString *) str
{
_string = str;
}
And in viewDidAppear, i am doing this:
- (void) viewDidAppear:(BOOL)animated
{
self.textLabel.text = [NSString stringWithString:_string];
NSLog(#"did appear called, str = %# and label text = %#", _string, self.textLabel.text);
}
and the log is showing that the value of the string is the passed text as it should be, but the value of textLabel.text is always null.
I have tried everything I could think of from checking that the UILabel is attached to the textLabel outlet to writing the code in viewWillAppear and viewDidLoad but nothing have worked so far. The string is showing the correct value but the label is not updating. What is wrong here?
Ok found the error:
It was a minor typo: I had declared IBOutlet as:
IBOutlet UILabel * textlabel;
and property as:
#property (nonatomic, strong) UILabel * textLabel;
and had attached the outlet to textlabel and had been updating textLabel. Fixed this and things are fine!

How to send text from text field to another view controller

I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextField and upon tapping the Send button, the value of the UITextField in ComposeViewController will be transferred to the table cell's UILabel in a custom cell in MasterViewController and also to the DetailViewController where another UILabel will get the text value from the UITextField. The DetailViewController is the ViewController loaded when the user taps the cells from the UITableViewCells.
I actually read related articles below but it doesn't work on my end.
How to send the text from textfield to another class?
How to see text from one text field in another text field?
How to send text field value to another class
Can you please guide me on how to properly implement this? I know it's easy. I just don't know why it's not working. ComposeVC is a ModalViewController while DetailVC is the view that loads when the user taps the cell in the MasterVC's table.
Thanks a lot!
Below is my code for ComposeVC.h:
UITextField *messageTextField;
#property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
for DetailVC.h
NSString *messageText;
#property (nonatomic, copy) NSString *messageText;
for DetailVC.m
#synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel is the UILabel inside my DetailVC.
You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
in view 2
.h file
#interface view2:UIViewController {
NSString *str;
}
#property (nonatomic, retain) NSString *str;
in .m file
#synthesize str;
Now you can send your text from view1 to view 2 like:
objView2.str = txtField.text;
For viewing one textfield's text in another use
secondTextField.text = firstTextField.text;
Hope this helped let me know if you are looking for something different.
take one variable in the app delegate like in appdelegate.h
#interface appdelegate
{
NSString *str1;
}
#property (nonatomic, retain) NSString *str;
In .m file synthesize it.
set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.
NSString *str = appdel.str(getting value);
Try this one:
#interface SecondView:UIViewController
{
NSString *stringSecond;
}
#property(nonatomic, retain) NSString *str;
In First View u have to create an reference like this:
#import "SecondView.h"
SecondView *detailViewController = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
detailViewController.stringSecond = #"Some string";

Change view load random text in labels

I'm using the arc4random command to change between three randomly selected views. In those three views, I have four labels displaying random texts when clicking on a button. So the labels are empty when you go to a random view until the button is clicked. But I need the four different labels to already be loaded with the random texts when going to the new view. How do I do that?
In your interface of randomly selected file put the following code.
In view1.h
#interface view1
{
NSString *strValue;
UILabel *lblText;
}
#property (nonatomic, retain) NSString *strValue;
In view1.m
#synthesize strValue;
- (void)viewDidLoad
{
lblText.text = strValue;
}
In your main view, from where you are calling the random view set following code...
- (void)btnClicked:(id)sender
{
view1 *objView1 = [[view1 alloc] initWithNibName:#"view1" bundle:nil];
objView1.strValue = #"Random Text";
[self.navigationController pushViewController:objView1 animated:TRUE];
}

Very simple iPhone App crashes on UILabel settext

I have a very simple application. I have a button and a label in IB. I have an IBAction for onClick that calls setText on the label. There's an outlet for the label. Everything is connected in IB. It crashes the app the first time in the simulator. When I launch it again, it sets the text. Then crashes again next time. It always crashes on the actual device. This should be simple, but I'm not sure what I'm doing wrong.
Thanks.
in my .h file :
#import <UIKit/UIKit.h>
#interface UntitledViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIButton *button;
}
#property (nonatomic, retain) UILabel *label;
-(IBAction) onClick1: (id) sender;
#end
and in the .m:
- (IBAction) onClick1: (id) sender
{
//[label setText:#"Hello World!"];
label.text = #"Hello World!";
//[button setTitle:#"Clicked" forState:UIControlStateNormal];
}
Sorry, I'm new to the site. How do I get the crash log and the stack? Thanks.
EDIT : While this answer is technically correct, it doesn't answer the question at all :( Sorry
< warning - this is a guess >
If you're getting a crash setting the label's text then it tells me that you have set a value to label in the past but it's not been retained correctly.
I'm guessing you have code like this :
label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];
when you should have code like
// Option 1
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)] autorelease];
or
// Option 2
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)];
( the first one uses the property to retain to label. The second one doesn't autorelease it. The first one is the recommended way )
Double check you set connection for label in IB.
Put breakpoint in debugger on line label.text = #"Hello World!";
And make sure label is not nil here.
If it is nil you didn't set connection in IB for it.