0

I am trying to make a test Application that when a user clicks a button, the text is toggled to something else and if its clicked again, it goes back but I also want to be able to add a section in which the user enters his/her names so it says , the hidden message was: or whatever...

The code I have is:

- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:@"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)button_method:(UIButton *)button {
NSString *test = @"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
[button setTitle:@"You have pressed the button!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(change_again:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)change_again:(UIButton *)button {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = @"You have found the next piece of text!";
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
[button setTitle:@"Keep pressing!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(button_method:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

I hope this isn't considered to be too localized!

muqman

PS: A side question, how would I create a string such as NSString *var4 = var.var2.var3;

what is the joining character? In JavaScript it's + in PHP it's .

user115422
  • 4,662
  • 10
  • 26
  • 38
  • For your side question: `NSString *var4 = [NSString stringWithFormat:@"%@%@%@", var, var2, var3];` (For your main question, I don't get exactly what you're asking. You say "global" but you may just need a property in your controller...not sure.) – Phillip Mills Nov 27 '12 at 02:08
  • @phillipMills ok im not too good with XCode terms, this is my first day programming... – user115422 Nov 27 '12 at 02:11
  • If those are string literals, you can use it as `NSString *var4 = @"some " @"text " @"here" @"!";` – iDev Nov 27 '12 at 02:12
  • @ACB so @var@var2@var 3? – user115422 Nov 27 '12 at 02:13
  • No, that will not work. Check this http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings – iDev Nov 27 '12 at 02:13
  • @acb so phillipmills method? – user115422 Nov 27 '12 at 02:14
  • Yes, that is the way you can combine them. Check the other question as well. You can see a lot of ways to do this, but nothing as simple as Java. – iDev Nov 27 '12 at 02:17
  • @ACB can you help me out with the other question? its my first day doing objective-c and ive never even done java, all ive done is PHP – user115422 Nov 27 '12 at 02:19
  • Check if you can use @property for saving the text. You can declare `@property(nonatomic, retain) NSString *text;` in .h file then use `self.text` to set and get the values. – iDev Nov 27 '12 at 02:25
  • I think that if you read this it will save you a lot of frustration: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html – Phillip Mills Nov 27 '12 at 02:40

3 Answers3

3

You probably want to use a @property to store the text input. Properties are like instance variables but they introduce another layer of abstraction by adding getters and setters retrieved whenever the ivars are retrieved (get) or assigned (set). At the moment all your variables only exist within your methods. A property will allow those variables to be accessed from different methods. Here's a crash course:

A property is declared in the @interface section (use the header file for properties accessible from other classes, use the main file for properties accessible only from your class).

    @property (strong, nonatomic) NSString *userInput;

In the @implementation section must write your own getter and setter or you can automatically create your getters and setters using synthesize:

    @synthesize userInput = _userInput;

From here you're able to use the property.

    //Assigning the property
    self.userInput = @"Some text";

    //Retrieving the property
    label.text = self.userInput;

I you want to learn all about properties, here's Apple's documentation.

One last thing about properties: Since they are instance variables, they are tied to the instance of the class and hence they won't persist between launches of your program.

For text input, try a UITextField.

If you're just starting programming iOS, I'd recommend Stanford's CS193p on iTunes U. I'm pretty sure he uses a custom getter and goes through the reasons you would want to write one when he writes a calculator app in lecture 2.

Javan Wood
  • 31
  • 2
  • how will i get the user input though? – user115422 Nov 27 '12 at 04:13
  • I mean how will i ask the button to set the variable of the value of the text field? – user115422 Nov 27 '12 at 04:16
  • In the button's callback method (button_method: or change_again:) you should be able to get the text from the UITextField using the UITextField's 'text' property. See the [class reference](http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html) for more information. – Javan Wood Nov 27 '12 at 04:41
1

There are two ways of using global variables. First, you can set a global variable (only available within that class and during the current session) in the Interface section of either your header or implementation method. Here's an example:

//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
    NSString *nameOfGlobalVariable;
}
@end

OR, in iOS you can store defaults with a storage system called NSUserDefaults. NSUserDefaults allow you to set and access defaults at any time, in any class, and during any app session. The defaults are stored in a PLIST file inside of your app's bundle. Here's how to set a default:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"YourText" forKey:@"NameOfDefault"];
[defaults synchronize];

You can also explore the NSUserDefaults method and try changing out setObject for something like setBool. Retrieving a default is very similar- simply define the NSUserDefaults, and use the [objectForKey] method to retrieve it. I'll leave you to figure that part out, because programing isn't fun when you just copy and paste ;)

Good luck!

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
  • ill come back to this in a bit, its late here :) i may need more explaining! Thanks for the answer, ill mark it if it solves my issue. – user115422 Nov 27 '12 at 02:40
0
Just try something like that:-

// In .h file, define your enum...
enum
{
MDefaultButtonText = 1,
MToggleButtonText
};

// In .m file, define your string if the text not changed...
#ifndef MDefaultButtonTextString
#define MDefaultButtonTextString "This is default string"
#endif

#ifndef MToggleButtonTextString
#define MToggleButtonTextString "This is toggled string"
#endif

/*
Please note, you can also used:-
const NSString *testStr = @"your required string";
// Feel free if you think to know more...
*/

Now, in viewDidLoad:-
[yourButton setTag:1];
[yourButton addTarget:self action:@selector(someProcedure:) forControlEvents:UIControlEventTouchUpInside];

Now, you do need of implementing your click event...
-(void)someProcedure:(id)sender
{
// Do your stuff here...

switch([sender tag])
{
case MDefaultButtonText://sender.tag = 1
     [sender setTag:2];
     [sender setTitle:MToggleButtonTextString forState:UIControlStateNormal];
     // In alert set Message to = MDefaultButtonTextString
     break;
case MToggleButtonText:////sender.tag = 2
     [sender setTag:1];
     [sender setTitle:MDefaultButtonTextString forState:UIControlStateNormal];
     // In alert set Message to = MToggleButtonTextString
     break;
default:
     //if you required...
     break;
}
}

Please cross check the required message accordingly to your requirement.Hope, it'll sort your issue.

Any concern, just get back to me. :)
Mohit_Jaiswal
  • 840
  • 6
  • 10