I've crate view controller and add a button as subview.
#implementation PredectiveViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setFrame:CGRectMake(0, 0, 400, 40)];
[self.view setBackgroundColor:[UIColor redColor]];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(200, 0, 200, 40)];
[b setTitle:#"Some button" forState:UIControlStateNormal];
[b addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
}
-(void)tapped:(id)sender
{
NSLog(#"Tapped");
}
Then set controllers view as input view
PredectiveViewController *pred = [[PredectiveViewController alloc] init];
[searchBar setInputAccessoryView:pred.view];
When i pressed the button first time, my program has crashed with error like
"[UIPopoverPresentationController tapped:] unrecognized selector
sent to instance"
But now i have no error on crash.
About hierarchy:
I present view controller A in UIPopoverController, view controller A have UITableViewController as subview, which have UISearchBar, which have inputAccessoryView with UIButton.
Running on iOS 8.
Any suggestion?
Related
I want to create a bottom tab that could change the view when user press.
To do that I create a view with view controller and sets it's frame in the init
Here is the bottom panel view controller
#implementation BottomTabViewController
-(id) initWithFrame:(CGRect)frame{
if(self = [super init]){
self.view.frame = frame;
return self;
}else{
return nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpButtons];
// Do any additional setup after loading the view.
}
-(void) featureBtnClick:(id*) sender{
NSLog(#"featureBtn Clicked");
}
-(void) favBtnClick:(id*)sender{
NSLog(#"Fav Btn Clicked");
}
-(void) setUpButtons{
UIButton *features = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[features setBackgroundImage:[UIImage imageNamed:#"feature.png"] forState:UIControlStateNormal];
features.backgroundColor = [UIColor greenColor];
[features addTarget:self action:#selector(featureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:features];
UIButton *favBtn = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width - 100, 10, 50, 50)];
[favBtn setBackgroundImage:[UIImage imageNamed:#"favorite.png"] forState:UIControlStateNormal];
[favBtn addTarget:self action:#selector(favBtnClick:) forControlEvents:UIControlEventTouchUpInside];
favBtn.backgroundColor = [UIColor greenColor];
[self.view addSubview:favBtn];
}
#end
And I added that to my view with code like this:
-(void) setUpBottom{
BottomTabViewController *botm = [[BottomTabViewController alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 55, [[UIScreen mainScreen]bounds].size.width, 55)];
botm.view.backgroundColor = [UIColor redColor];
// botm.view.userInteractionEnabled = false;
botm.view.userInteractionEnabled = true;
[self.view addSubview:botm.view];
}
Here here is the result seems, note the bottom panel that works, well pretty silly, but it shows up:
But when press the left and right button, there is not log printed which it expected to be to indict that the button works, What have I done wrong? Thanks
you are adding buttons in the view first then adding the bottom view.
if this is what you are doing then it can be overlapped by the bottom view.
In this case buttons cant be tapped because they have bottom view on top of them.
so make sure u add bottom view first then your buttons. (or change their frames to avoid overlapping)
I have one home screen. And i programatically create two button with action. One will go to sign up screen , and another will go to sign in screen.
I added the both sign in and sign up storyboard identity. But when i move from that home VC to sign in VC and Sign Up VC i am getting crash:
-[ViewController pressSignUp:]: unrecognized selector sent to instance 0x7fbb3a72e3f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController pressSignUp:]: unrecognized selector sent to instance 0x7fbb3a72e3f0'
*** First throw call stack:
Here is my code:
// create button 1
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button addTarget:self action:#selector(pressSignUp:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button];
// Button Two
UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button1 setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button1 setTitle:#"Sign In" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(pressSignIn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button1];
// Border color for one button
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 1, button.frame.size.height)];
leftBorder.backgroundColor = [UIColor colorWithRed:237/255.0f green:211/255.0f blue:246/255.0f alpha:0.6f];
[button1 addSubview:leftBorder];
-(void)pressSignUp
{
UIViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"SignUpViewController"];
[self presentModalViewController:secondViewController animated:YES];
// Here You can write functionality
}
-(void)pressSignIn
{
UIViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"SignInViewController"];
[self presentModalViewController:secondViewController animated:YES];
// Here You can write functionality
}
Why i am getting that crash.It any possible to move to Another Vc.
Please help thanks
You forget to implement the parameter for the sender:
-(void)pressSignUp:(UIButton*) sender {
}
-(void)pressSignIn:(UIButton*) sender {
}
And maybe you did not set the identifier in the interface builder:
It's under the Identity Inspector tab in IB. It's called "Storyboard ID". E.g.: SignUpViewController
I have a strange issue with iOS UIButton.
I have a UIViewController class:
#interface CustomViewController : UIViewController
Inside of this class I have a label called customLabel. In this label I have a button subview:
UIButton *addButton = [[UIButton alloc] init];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(customButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[customLabel insertSubview:customButton atIndex:1];
And of course, action method:
- (void)customButtonClicked
{
NSLog(#"- (void)customButtonClicked");
}
I also have a subclass for this view controller:
#interface MyNewCustomViewController : CustomViewController
This view controller is loaded inside UIWindow:
[self.navigationController pushViewController: myNewCustomViewController animated:YES];
Everything seems to be fine, label with button are at the correct positions, except the fact, that button click doesn't call action method...
Maybe you know, where the problem could be?
As long as you don't have another view over top of your current button and user interaction is enabled on the view. This should work.
// First make sure userInteractionEnabled is enabled for your custom label which will contain your button as a subview
customLabel.userInteractionEnabled = YES;
// Then create and add your button as a subview
UIButton *addButton = [[UIButton alloc] init];
[customButton setImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(customButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[customLabel addSubview:addButton]; // Places view on top index
I have one UIButton. Now I add button on Subview :-
UIButton* button4 = [[UIButton alloc] initWithFrame:frame4];
[button4 setBackgroundImage:image3 forState:UIControlStateNormal];
[button4 setShowsTouchWhenHighlighted:YES];
[button4 addTarget:self action:#selector(Nofication:) forControlEvents:UIControlEventTouchDown];
- (void)Nofication:(id)sender
{
share=[[UIView alloc]init];
share.frame=CGRectMake(300, 60, 130, 100);
[self.view addSubview:share];
[UIView animateWithDuration:2.0
animations:^{
share.frame =CGRectMake(180, 50, 130, 100); // its final location
}];
share.backgroundColor=[UIColor yellowColor];
login=[[UIButton alloc]init];
login.frame=CGRectMake(0, 0, 130, 50);
[login setBackgroundImage:[UIImage imageNamed:#"login.png"] forState:UIControlStateNormal];
[login addTarget:self action:#selector(asubmit:) forControlEvents:UIControlEventTouchDown];
[share addSubview:login];
login.layer.borderColor = [[UIColor whiteColor]CGColor];
policies=[[UIButton alloc]init];
policies.frame=CGRectMake(0, 50, 130, 50);
[policies setBackgroundImage:[UIImage imageNamed:#"Policies.png"] forState:UIControlStateNormal];
[policies addTarget:self action:#selector(apolicies:) forControlEvents:UIControlEventTouchDown];
[share addSubview:policies];
}
When I click button it's open subview working nice. But now I need if subview is showing in that time click button it's need to hidden subview. If subview is not showing in that time when I click button it's need to show subview. So Please give me any idea about it.
How to Show and Hidden same UIButton
thanks in Advance .
There is a hidden property on UIView. https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/hidden
if (button.isHidden) {
button.hidden = NO;
}
Is it what asked?
Where subview is the view (or UIButton) you are trying to hide if shown, and show if hidden.
[subview setHidden:![subview isHidden]];
Edit :
In your Notification function :
// Be sure that the view is loaded, maybe load it at a different place or use a boolean
if([subview isHidden]){
[subview setHidden:false];
}else{
[subview setHidden:true];
}
You can set button4.selected = !button4.selected in Nofication: method.
Then you can check button4.isSelected and hide/show subview with hidden property
Hope it helps.
I am trying to programmatically display a view containing an image and an "x" button -- in the code it is labeled *btnDismiss -- to close the view.
Here is my issue:
The code will successfully display the image and the "x" button. However, clicking the "x" button currently doesn't close the view -- Both the image and the "x" button remain visible.
I'm new to xCode, so please provide verbose responses (if you leave out something from your answer, I might not be able to fill in the blanks!)
Note: If you have another solution for how to show an imageView with an "x" button to make both the imageView and the "x" button disappear, that will also be welcome.
Here is my code:
#import "XYZViewController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
bgImage.image = [UIImage imageNamed:#"image"];
[self.view addSubview:bgImage];
UIButton *btnDismiss = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
[btnDismiss setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnDismiss setTitle:#"x" forState:UIControlStateNormal];
[btnDismiss addTarget:self action:#selector(dismissVC:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnDismiss];
}
-(void)dismissVC:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Thanks in advance!
Your code is set up to dismiss a view controller that was presented modally. But the views you want to remove are subviews of self.view.
You can remove the UIImageView and UIButton by keeping outlets to them and calling removeFromSuperview in your remove method. With this method, you don't need to present or dismiss another view controller.
-(void)removeViews:(id)sender {
[self.xButton removeFromSuperview];
[self.imageView removeFromSuperview];
}
Edit in response to comment
You'll need to have properties declared for xButton and imageView
#property UIButton *xButton;
#property UIImageView *imageView;
Edit to show full code
#interface MJNViewController ()
#property UIButton *xButton;
#property UIImageView *imageView;
#end
#implementation MJNViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 200, 200)];
self.imageView.image = [UIImage imageNamed:#"myImage"];
[self.view addSubview:self.imageView];
self.xButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
[self.xButton setTitle:#"x" forState:UIControlStateNormal];
[self.xButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.xButton addTarget:self action:#selector(removeViews:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.xButton];
}
-(void)removeViews:(id)sender
{
[self.imageView removeFromSuperview];
[self.xButton removeFromSuperview];
}
#end
You need to have another UIViewController presenting XYZViewController in order for -dismissViewControllerAnimated: to work properly.
From the Apple Docs:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
Since there is no view controller being presented, calling -dismissViewControllerAnimated: is not working for you.