executing an action in a different view controller - ios

In my application, I am trying to create a button, with a button. My code right now is as follows:
- (IBAction)doneTextField:(id)sender {
YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
YSVC2.stringFromTextField = self.textField.text;
[self presentViewController:YSVC2 animated:YES completion:nil];
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn addTarget:self action:#selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"FaceTime" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
}
this works, but I need the button that is being added to add in a different view controller, not the view controller that the code is in.
How would I do this??
Thank you

You need to create a function to add the button in the new view controller:
In YourShortcutsViewController.h:
-(void)addFaceTimeButton:(UIButton*);
In YourShortcutsViewController.m:
-(void)addFaceTimeButton:(UIButton*){
[self.view addSubview:btn];
}
And then change your code to:
- (IBAction)doneTextField:(id)sender {
YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
YSVC2.stringFromTextField = self.textField.text;
[self presentViewController:YSVC2 animated:YES completion:nil];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn addTarget:self action:#selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"FaceTime" forState:UIControlStateNormal];
[YSVC2 addFaceTimeButton:btn];
}

Related

How I can add action to title of NavigationBar?

I want to give/add action to the title of navigation title.
(Example "Explore")
I want to perform some activity using this approach.
I tried following code, but it's not working.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
//[view addSubview:button];
[self.navigationItem.titleView addSubview:button];
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
ViewController name is came from presentViewController type not from push.navigationController stack.
How I can achieve this using objective-C code?
Replace your code with the following:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
self.navigationItem.titleView = button;
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
titleView is nil by default.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview
You should set your title view with your button. Not add it as a subview.
self.navigationItem.titleView = button

Button on scrollView lost action event

How can I get action event on this button ?
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.scrollView];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 32, 32);
backButton.adjustsImageWhenHighlighted = YES;
[backButton setImage:backButtonImage forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:backButton];
your code is good. implement this function
- (void)backButtonAction:(id)sender
{
NSLog(#"backButton pressed");
}
if it not called so check userInteractionEnabled for scroolView and set to YES
_scrollView.userInteractionEnabled = YES;

Fetching the tag values on selector

I am working with the iphone project. In that i added 3 buttons with same selector name(i.e action). Now i am fetch the data from the database using the query. But it is showing me the data to 3rd button only when i am pressing any butoon out of three .
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 30, 200, 50);
[btn.layer setBorderWidth:0];
btn.tag = 1;
[btn setTitle:#"1" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(detail:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:#"Zapfino" size:14.0];
[scrollview addSubview:btn];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 30, 200, 50);
[btn.layer setBorderWidth:0];
btn.tag = 2;
[btn setTitle:#"2" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(detail:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:#"Zapfino" size:14.0];
[scrollview addSubview:btn];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 30, 200, 50);
[btn.layer setBorderWidth:0];
btn.tag = 3;
[btn setTitle:#"3" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(detail:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:#"Zapfino" size:14.0];
[scrollview addSubview:btn];
this is the action which i am using.
-(IBAction)detail:(id)sender
{
detailViewController *detailvc =[[detailViewController alloc]initWithNibName:#"detailViewController" bundle:Nil];
detailvc.btntxt = btn.tag;
NSLog(#"name of btn :%ld",(long)btn.tag);
[self.navigationController pushViewController:detailvc animated:YES];
}
In nslog also i am getting the tab of 3rd button only
please help me out regarding this issue...
You are assigning 1 tag to a button
btn.tag = 1;
add different tag to each button
--
Also in your detail: take the tag from the sender and not your instance variable
- (IBAction)detail:(UIButton *)sender {
detailViewController *detailvc =[[detailViewController alloc]initWithNibName:#"detailViewController" bundle:Nil];
detailvc.btntxt = sender.tag;
NSLog(#"name of btn :%ld",(long) sender.tag);
[self.navigationController pushViewController:detailvc animated:YES];
}
You set the tag number of all 3 buttons to the same value (1). You need to use different values if you want to be able to tell your buttons apart.
Use tag 1 for the first button, tag 2 for the second, and tag 3 for the third.
Use the sender variable to get the button that triggered the action:
-(IBAction)detail:(id)sender
{
UIButton *button = (UIButton *)sender;
NSInteger buttonTag = button.tag;
// etc
}
Also, give each button it's own tag
btn.tag = 2; // and 3
And don't use the btn variable in the action method. It will only ever contain the last set variable, which is the third button in this case.
just use ((UIButton)sender).tag instead of btn.tag
The main problem with your code is that you are using just single button object ( & initializing it three times & adding three different tags & adding it to subviews.), which looks like an ivar.
So even if you have added three different tags to a single object instance the recent most will be reflected & so you are getting just 3 as tag, all time.
btn = [UIButton buttonWithType:UIButtonTypeCustom];
....
btn.tag = 1;
.
.
btn = [UIButton buttonWithType:UIButtonTypeCustom];
...
btn.tag = 2;
.
.
btn = [UIButton buttonWithType:UIButtonTypeCustom];
...
btn.tag = 3
Instead you either don't use btn object in ur target method or you can have three different buttons for a good understanding.
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(80, 30, 200, 50);
....
btn1.tag = 1;
.
.
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(80, 80, 200, 50);
...
btn2.tag = 2;
.
.
UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(80, 130, 200, 50);
...
btn3.tag = 3
Rest you need to modify the code of the target method as
-(IBAction)detail:(UIButton *)sender
{
detailViewController *detailvc =[[detailViewController alloc]initWithNibName:#"detailViewController" bundle:Nil];
detailvc.btntxt = sender.tag;
NSLog(#"name of btn :%ld",(long)sender.tag);
[self.navigationController pushViewController:detailvc animated:YES];
}
I hope this will certainly help you.
-(IBAction)detail:(UIButton*)sender
{
if(sender.tag==1)
{
}
else if(sender.tag==2)
{
}
else if(sender.tag==3)
{
}
}

ACTION for UIBUTTON in iOS

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button
If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];
Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];
Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

how to load same tableview with different NSArrays depending on uibuttons tapped?

In my app i have a scroll-view and a table-View added as a sub-view and on this scroll view i have multiple menu buttons (UI Button) added as sub-view to my scroll-view.i want to load the table-View with different arrays depending on respective menu button is tapped.Any Ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
[self PutButtoninScrollView:7];
}
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
myButton1=[[UIButton alloc]init];
myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
myButton1.frame=CGRectMake(5, 5, 70, 30);
[myButton1 setTitle:#"दुनिया" forState:UIControlStateNormal];
[myButton1 setTag:1];
myButton1.backgroundColor=[UIColor blackColor];
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton1];
myButton2=[[UIButton alloc]init];
myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2.frame=CGRectMake(80, 5, 70, 30);
[myButton2 setTitle:#"India" forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton2];
[self.myScrollView setContentSize:CGSizeMake(160, 35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([myButton.currentTitle isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([myButton.currentTitle isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
problem is when i execute the code
"successful" is displayed. that means ListViewAction method is getting called but my if statement is never executed.
try this
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
xPosition=5;
for(int i=0;i<numberOfbuttons;i++)
{
myButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(xPosition, 5, 70, 30);
myButton.backgroundColor=[UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
[myButton setTag:i];
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton];
xPosition+=75;
}
[self.myScrollView setContentSize:CGSizeMake(xPosition,35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
- (void) buttonClicked:(id) sender
{
NSString *strTitle = [sender currentTitle];
// arrItem have list of array items
NextView *detailViewController = [[NextView alloc] initWithNibName:#"NextView" bundle:nil];
detailViewController.arrTableView =[arrItem objectAtIndex:[strTitle integerValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([[sender currentTitle] isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([[sender currentTitle] isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
try this
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];

Resources