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

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
}

Related

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];
}
}

Dismissing the keyboard with a uibutton when there are multiple text fields? [duplicate]

This question already has an answer here:
resignFirstResponder for all textfields [duplicate]
(1 answer)
Closed 8 years ago.
I know that I can resign a single text field to first responder when touching a button by implementing the following:
- (IBAction)signMeUpButton:(id)sender {
[self.textfield resignFirstResponder];
}
However, In my case I have multiple text fields, and I feel like typing them in one at a time, cant possibly be the best way / most modern way of doing so.
I reviewed multiple questions on the site, such as this one:
iOS SDK: Dismiss keyboard when a button gets clicked?
But none of them mention, having more than one text field and dismissing them with an IBAction ... What can I do in this situation?
You may try this:
- (IBAction)signMeUpButton:(id)sender
{
[self.view endEditing:YES];
}
If you have more than one text field, it's best to implement the following
[self.view endEditing:YES];
So, in your case it would be :
- (IBAction) signMeUpButton:(id)sender {
[self.view endEditing:YES];
}
That will do exactly what you are looking for!
try this
UITextField *currentTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
-(IBAction)click:(id)sender
{
[currentTextField resignFirstResponder];
}

How to create a tip balloon? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my image.I want to create a tip balloon like this. This must visible when user types in textfield
I have created one more but that is in cloud shape. Please help to create this
Subclass the UITextField then override becomeFirstResponder (show bubble) and resignFirstResponder (hide bubble) and when the field is active present your bubble.
.h
#import <UIKit/UIKit.h>
#interface BubbleTextField : UITextField
#end
.m
#import "BubbleTextField.h"
#interface BubbleTextField ()
{
UIView *_bubbleView;
}
- (void)showBubble:(BOOL)show;
#end
#implementation BubbleTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialize the _bubbleView here
}
return self;
}
#pragma mark - Override
- (BOOL)becomeFirstResponder {
BOOL shouldBecome = [super becomeFirstResponder];
if (shouldBecome)
{
[self showBubble:YES];
}
return shouldBecome;
}
- (BOOL)resignFirstResponder {
BOOL shouldResign = [super resignFirstResponder];
if (shouldResign)
{
[self showBubble:NO];
}
return shouldResign;
}
#pragma mark - Private Methods
- (void)showBubble:(BOOL)show {
// Show/Hide
// Animations etc.
}
#end
Check out KBPopupBubble.
And CMPopTipView
Also, check out these search results for custom controls on cocoa controls. Search Results.

How to perform two different functions for a single uibutton action [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a UIButton and a action to it.Now i need to perform some task when user clicks the button for the first time and another set of statements when user clicks for the second time.How can i do this?
Thanks in advance
Not understand what's your issue but according to question title you crate two Method that you want to perform alternate in single button Action click event for example:-
-(void)MethodFirst
{
// your code
}
-(void)Methodsecond
{
// your code
}
-(IBAction)Yourbuttonclick:(UIButton *)sender
{
sender.selected=!sender.selected;
if (sender.selected)
{
[self MethodFirst];
}
else
{
[self Methodsecond];
}
}
define a static int counter = 0 in your function, and increment the counter each time the button is pressed eg, counter++; then have an if statement
if(counter == 0){
//do first set of instructions
}
else {
//do second set
}
Suppose the below one is your action. Take a bool variable globally say BOOL isalreadyClicked; and set it as NO.
- (void)buttonClicked:(UIButton *)sender
{
if(isalreadyClicked)
{
/// your work here
isalreadyClicked=NO;
}
else
{
/// your work here
isalreadyClicked=YES;
}
}
Hope this will help
give a tag to button for firsttime and check tag in action method
egg
button.tag = 100; //firsttime
-(void)buttonaction:(UIButton*)sender{
if(sender.tag == 100){
// do something for first time
sender.tag = 200;
}
else if(sender.tag == 200) { //second time
// do something for second time
}
}
Use a BOOL value.
In your .m:
-(IBAction)methodButton{
self.flag = !self.flag;
if (self.flag == YES){
//Here do your first task
}
if (self.flag == NO){
//Here do your second task
}
}

Change button title [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
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];
}

Resources