Change button title [closed] - ios

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i created nine buttons using IBOutletCollection here.
My question is - i want to get title which button is pressed and display it on another buttons title. Is this possible!

Try like this,
- (IBAction)buttonAction:(UIButton *)btn {
yourNextVCobject.buttonTitle = [NSString stringWithFormat:#"%#",btn.titleLabel.text];
// then push to ur vc.
}
In your Next VC,
In .h
#property (nonatomic, retain) NSString *buttonTitle;
In .m
[yourAnotherBtn setTitle:self.buttonTitle forState:UIControlStateNormal];

You can use the below code to set the title of clicked button to targetted another button.
-(IBAction)clickbutton:(id)sender
{
UIButton *firstButton = (UIButton *)sender;
NSString *neededtitle = [NSString stringwithformat:#"%#",firstButton.currentTitle];
[anotherbutton setTitle:neededtitle forState:UIControlStateNormal];
}

Related

How to create many similar ViewControllers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to make a simple ViewController with two or three Buttons and labels. When I press the Button it plays an audio file.
I want to make hundreds of this similar screen, what is the best way to create it? (I am currently creating it with MainStoryBoard.)
For each page I would like to make small changes such as Buttons sizes ,Buttons numbers, label texts and audio files.
Drawing text bubbles or using xib file might be good, but I am not sure what I should.
You first need to have your own class for this UIViewController as a base class that has buttons, labels, etc.
Then use Factory Design pattern to generate the inherited UIViewController class which allows you to do some tweak that fits you need.
Factory Design Pattern
BaseViewController {
UIButton *button1;
UIButton *button1;
UILabel *label1;
}
ChildViewControllerA : BaseViewController {
UIButton *button3
}
ChildViewControllerB : BaseViewController {
UIButton *button4
}
Factory : NSObject {
+ (BaseViewController)generateChildViewController: (int) type {
switch (type)
case 0:
return [[ChildViewControllerA alloc] init];
case 1:
return [[ChildViewControllerB alloc] init];
}
}
Main {
- (void)createThreeViewControllers {
BaseViewController *vc1 = [Factory generateChildViewController:0];
vc1.button1.backgroundColor = [UIColor blueColor];
BaseViewController *vc2 = [Factory generateChildViewController:0];
vc2.button2.center = cgpointmake (100, 150);
BaseViewController *vc3 = [Factory generateChildViewController:0];
vc3.label1.text = #"vc3 test";
}
}

iOS: How to change UIImage with UIButton [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This might be simple, but I'm very new at developing an iOS APP. I have an UIButton and three UIImage's. What I'm trying to do is that when the user taps the button, the first image will display, if the user taps it again, then show the second image, and so on. When the last image is displaying, then go back to the first one when tapping the button again. The problem is that only the first image appears.
Here is my code:
file.h
#interface ViewController : UIViewController
{
IBOutlet UIImageView *images;
}
-(IBAction)changeImage:(id)sender;
#end
file.m
#implementation ViewController
-(IBAction)changeImage:(id)sender{
images.image = [UIImage imageNamed:#"pic1.png"], [UIImage imageNamed:#"pic2.png"],[UIImage imageNamed:#"pic3.png"];
}
Any help would be greatly appreciated!
Than you in advance!
You can declare a global variable for the index and global variable array to hold the image names
self.index = 0;
self.imageList = [NSArray arrayWithObjects:#"pic1", #"pic2", #"pic3", nil];
then in the click action, you can do
-(IBAction)changeImage:(id)sender{
images.image = [UImage imageNamed:self.imageList[self.index]];
self.index = (self.index + 1 ) % [self.imageList count];
}
I don't have the compiler with me to verify the syntax, but you get the idea.
This is easy, create a counter called tapCounter in viewDidLoad. Let's imagine you already set your image array as follows in viewDidLoad. Make sure images and tapCounter are properties of your class
- (void)viewDidLoad {
[super viewDidLoad];
self.images = #[[UIImage imageNamed:#"pic1.png"], [UIImage imageNamed:#"pic2.png"],[UIImage imageNamed:#"pic3.png"]];
self.tapCounter = 0;
}
-(IBAction)changeImage:(id)sender {
UIButton *but = (UIButton *)sender;
tapCounter++;
if (tapCounter == 4) {
tapCounter = 1;
}
[but setBackgroundImage:[images objectAtIndex:tapCounter-1] forState:UIControlStateNormal];
}
}

how to use a void inside a case? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have a code like this...
in the .h
- (void)job1:(id)sender;
in the .m
i have a UIButton and also a case switch. and i want to make one of the cases, lets say case 1, have the same job like that button.
so I said that the action is the "job1" in the void.
and i wrote like this for the button:
- (IBAction)button:(id)sender {
[_button addTarget:self action:#selector(job1:) forControlEvents:UIControlEventTouchUpInside];
}
And now in the case, i want case 1 to do the same thing.. "job1" from that void.
(i use the cases for table view, so each case is a cell)
switch(row){
case 0:
//doesn't matter
break;
case 1:
// I want the same thing to happen here when this cell is pressed.
// so how do i do that?
// can i address the void "job1"?
// if not, can i address the button ?
break;
every help is appreciated.
thanks.
(I edited it to be more clear).
Move the code outside of the button logic
- (IBAction)button:(id)sender {
[self doStuff];
}
- (void) caseFunction {
switch (val) {
case 0:
[self doStuff];
break;
}
}
- (void) doStuff {
// Code goes here
}

Implementing searchBarSearchButtonClicked [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How would I implement the searchBarSearchButtonClicked method? I am trying to pass the value of the search bar when the searchBarSearchButtonClicked to a label.
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
yourlabel.text = searchBar.text;
// DO what ever you want
}
Like so:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
_myLabel.text = searchBar.text;
}
That's it!

Add UITextFieldDelegate in cocos2d? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
There are a lot of similar questions and answers, but they don't solve the main problem: how to properly add any text field in cocos2d with its workable delegate.
For example if I create UITextfield and simply add it to [[CCDirector sharedDirector] view] then it will call textFieldShouldReturn: method only. No more methods doesn't work, even textViewDidBeginEditing:
I gave the same result with using of separate view and UITextField. I also tried to create a separate UIViewController to use its view with UITextField but it even make my app falling down
1: Make sure your CCNode contains the UITextFieldDelegate:
#interface MyLayer : CCLayer <UITextFieldDelegate>
2: Init the UITextField as per usual:
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, fieldWidth, fieldHeight)];
nameField.textAlignment = NSTextAlignmentCenter;
nameField.font = [UIFont fontWithName:kFontName size:24];
nameField.text = #"Enter Name";
nameField.delegate = self;
nameField.backgroundColor = [UIColor grayColor];
nameField.textColor = [UIColor whiteColor];
nameField.autocorrectionType = UITextAutocorrectionTypeNo;
3: Add the field to the CCDirector view:
[[[CCDirector sharedDirector] view] addSubview:nameField];
4: Implement the UITextField delegates in your CCNode:
- (BOOL)textFieldShouldReturn:(UITextField*)textField
- (void)textFieldDidBeginEditing:(UITextField *)textField
My mistake was that UITextFieldDelegate and UITextViewDelegate have some similar mehods, and I used some methods from the second delegate. Example:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Of course I wrote (UITextView *)textField at the end.

Resources