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;
Related
I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}
Adding two button via coding in viewDidLoad section is always hiding one button, and I don't know why this is happening.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIImage *addButton=[UIImage imageNamed:#"add.png"];
add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[add setImage:addButton forState:UIControlStateNormal];
[self.view addSubview:add];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[add setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
This looks like a copy and paste error. You never set the frame, image, or target action for the second button because you have "add" where you should have "cntctBtn".
- (void)viewDidLoad {
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addButton.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[addButton addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[addButton setImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
[self.view addSubview: addButton];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cntctBtn.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[cntctBtn addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[cntctBtn setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
Its a copy paste error. Above is the corrected code.
i am creating three buttons and one uitextview dynamically but when i run the program its not display on my screen
this my code to create dynamic buttons and textview
-(void)getSmsdisplay
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextView *textview=[[UITextView alloc]init];
[button setTitle:#"Comment" forState:UIControlStateNormal];
[button1 setTitle:#"Share" forState:UIControlStateNormal];
[button1 setTitle:#"Like" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self
action:#selector(btncomment:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self
action:#selector(btnShare:)forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(Like:)forControlEvents:UIControlEventTouchUpInside];
smsdata *data=[[smsdata alloc]init];
[textview setText:data.Data];
[self.view addSubview:textview];
//add the button to the view
[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:button2];
}
your coding is fine , but u r not added the frame for UIButton and UItextview
-(void)getSmsdisplay
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextView *textview=[[UITextView alloc]init];
button.frame=CGRectMake(20, 50, 200, 50);
button1.frame=CGRectMake(20, 150, 200, 50);
button2.frame=CGRectMake(20, 300, 200, 50);
button.backgroundColor=[UIColor redColor];
button1.backgroundColor=[UIColor grayColor];
button2.backgroundColor=[UIColor blackColor];
[button setTitle:#"Comment" forState:UIControlStateNormal];
[button1 setTitle:#"Share" forState:UIControlStateNormal];
[button2 setTitle:#"Like" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self
action:#selector(btncomment:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self
action:#selector(btnShare:)forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(Like:)forControlEvents:UIControlEventTouchUpInside];
[textview setText:#"karthik"];
[self.view addSubview:textview];
//add the button to the view
[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:button2];
}
the output is
You are assigning self.view to another UIView instance, which has not been added to the viewController. Note that self.view is already initialised in a viewController and it is dangerous to reassign the same.
Also, you have not set any frames for the Buttons and textview.
The following code should make it work.
-(void)getSmsdisplay
{
UIView *smsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]];
[self.view addSubview: smsView]; //The new view needs to be added to something!
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextView *textview=[[UITextView alloc]initWithFrame:CGRectMake:(20,20,100,50)];
[button setTitle:#"Comment" forState:UIControlStateNormal];
[button1 setTitle:#"Share" forState:UIControlStateNormal];
[button1 setTitle:#"Like" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self
action:#selector(btncomment:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self
action:#selector(btnShare:)forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(Like:)forControlEvents:UIControlEventTouchUpInside];
smsdata *data=[[smsdata alloc]init];
[textview setText:data.Data];
[smsView addSubview:textview];
button.frame=CGRectMake(20, 50, 200, 50);
button1.frame=CGRectMake(20, 150, 200, 50);
button2.frame=CGRectMake(20, 300, 200, 50);
//add the button to the view
[smsView addSubview:button];
[smsView addSubview:button1];
[smsView addSubview:button2];
}
I want to create the view with button, textbox, label programatically in viewdidload
But touch events are not working on view and its subviews even though i have set userinteractionenabled to YES.
Here is my code:
CGRect bounds = [[UIScreen mainScreen] bounds];
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventAllTouchEvents];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
try this
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tappedOnView:)]; //add the gesture to get the touch event
tapGesture.numberOfTapsRequired = 1;
[mainView addGestureRecognizer:tapGesture];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
modify the line
//UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
to
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
and add the method in your code
-(void)buttonClicked:(UIButton *)button
{
NSLog(#"hai");
}
and select the checkbox enable user interaction in interface builder its working fine.
I have added a few buttons to the right side of the navigation bar with the following:
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor clearColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"toc.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(tableOfContentsAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"bookmark.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(bookmarkButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[customView release];
[segmentBarItem release];
This works well. For both buttons I show a popOver as shown below
- (void) bookmarkButtonAction
{
BookmarksViewController* content = [[BookmarksViewController alloc] initWithOrientation:lastOrientation selectedPage:selectedPage];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:content];
CGSize size = content.view.frame.size;
aPopover.popoverContentSize = size;
aPopover.delegate = self;
self.bookmarksPopoverVC = content;
self.bookmarksPopoverVC.popUpController = aPopover;
[content release];
[aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[aPopover release];
bookmarksShowing = YES;
}
The problem is that I am using presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem and this shows the top arrow in the middle of the two buttons. How can I attach the arrow to each button?
instead of using this line:
aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem
You may better try this line:
aPopover presentPopoverFromBarButtonItem:sender
I think that would solve your problem
try this:
- (IBAction)products:(id)sender {
UIButton* btn = (UIButton *)sender;
[productsPopover presentPopoverFromRect:[btn bounds] inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Works like a charm