Cannot get UIButton to programmatically call action in iOS - ios

I cannot get my button to fire the associated method. I am not using IB or anything like that (all programmatic). The _mainViewController is passed in during custom init, just to clarify (although this should have nothing to do with the button). Please see my code snips:
viewcontroller.h
#import <UIKit/UIKit.h>
#interface CSNWZSSViewController : UIViewController
- (void)doneButtonPressed;
#property UIViewController *mainViewController;
#property UIButton *doneButton;
#end
viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mainViewController.view.frame.size.width, 45)];
headerView.backgroundColor = [UIColor darkGrayColor];
_doneButton = [[UIButton alloc] initWithFrame:CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height)];
[_doneButton addTarget:self
action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:#"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
[headerView addSubview:_doneButton];
CGRect headerLabelFrame = CGRectMake(5, 5, headerView.frame.size.width - 105, headerView.frame.size.height - 5);
UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerLabelFrame];
headerLabel.text = #"test header";
headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont systemFontOfSize:20.0f];
headerLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[headerView addSubview:headerLabel];
[self.view addSubview:headerView];
}
- (void)doneButtonPressed
{
NSLog(#"button pressed");
}
Update 10/9/16 using buttonWithType init and explicit userInteractionEnabled
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_doneButton addTarget:self
action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:#"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor blackColor]; //[UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
_doneButton.frame = CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height);
_doneButton.userInteractionEnabled = YES;
[headerView addSubview:_doneButton];
Note 12/9/16 the _mainViewController object is passed in here:
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
Solution with matt's help (see below) The context was getting messed up due to the lack of adding the new view controller.
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController addChildViewController:CSNWZSSVC];
[self.viewController.view addSubview:CSNWZSSVC.view];

The _mainViewController is passed in during custom init
I'm going to guess that this is the problem — that you are mismanaging this view controller and that it is not being correctly made part of the view controller hierarchy, and in fact may even be going out of existence so that there is no one to send the button action message to.
edit Yes, now that you've posted more code, I'm clearly right:
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
That is totally wrong behavior. You cannot arbitrarily add another view controller's view to yours. There is a strict dance for managing a child view controller, and you are not doing the dance.

Try:
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
And then set your frame.

Related

Button Text not showing

When I try to post the following code, my app just shows an empty rectangle where the button's text should be. I am a beginner at xcode and I have determined this code from other tutorials.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *player1 = [[UILabel alloc]initWithFrame:CGRectMake(35, 120,130, 30)];
[player1 setText:#"Player"];
player1.layer.borderWidth = 1.0;
player1.layer.borderColor = [UIColor blackColor].CGColor;
player1.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player1];
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
[score1 setTitle:#"0" forState:UIControlStateNormal];
score1.layer.borderWidth = 1.0;
score1.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: score1];
UILabel *player2 = [[UILabel alloc]initWithFrame:CGRectMake(35, 150, 130, 30)];
[player2 setText:#"Opponent"];
player2.layer.borderWidth = 1.0;
player2.layer.borderColor = [UIColor blackColor].CGColor;
player2.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player2];
}
Like I said, everything but the text in the button is showing up.
The reason why the button looks blank is because its text color is actually white. Just give it a different color and 0 will now show:
score1.layer.backgroundColor = [UIColor blackColor].CGColor;
If you don't want to change the button background color, you may want to set the text color:
[score1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Read Question: [initWithFrame:frame] vs. [UIButton buttonWithType], for more about creating a UIButton with these different methods.
The problem is the way you're creating the button:
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
That's not how you do it. Do it like this:
UIButton *score1 = [UIButton buttonWithType:UIButtonTypeSystem];
score1.frame = CGRectMake(165, 120, 60, 30);
All will be well after that!

Displaying a custom line over view

Hi I want to show a line in over view like given image.
But when i am doing this i m getting line in complete view .
My code-
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(2,30, _MovetoSearch.frame.size.width, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];
How to make line like above image ?
When you set child view frame that time always set it using parent view frame so if in future you can change parent frame then no need to change child frame its automatically change as per parent view like I write as follow.
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_MovetoSearch.frame.origin.x+2,_MovetoSearch.frame.size.height-5, _MovetoSearch.frame.size.width-30, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];
I assume your searchBtn is having similar kind of code
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[searchBtn setFrame:CGRectMake(_MovetoSearch.frame.size.width-32, 0, 32, 32)];
[searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
[searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateHighlighted];
[searchBtn addTarget:self action:#selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];
For your UITextField should be
CGFloat fieldHeight = searchBtn.frame.size.height-2;
CGFloat fieldWidth = _MovetoSearch.frame.size.width - (searchBtn.frame.size.width + 4);
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(2, 0, fieldWidth, fieldHeight)];
[textfield setBackgroundColor:[UIColor clearColor]];
[textfield setBorderStyle:UITextBorderStyleNone];
[textfield setNeedsDisplay];
[_MovetoSearch addSubview:textfield];
for line view
CGFloat fieldHeight = searchBtn.frame.size.height-2;
CGFloat fieldWidth = _MovetoSearch.frame.size.width - (searchBtn.frame.size.width + 4);
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(2, fieldHeight+1, fieldWidth, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];
Hope it will work for you
I would suggest to use layer for such task and not overuse UIView.
CALayer * lineLayer = [CALayer alloc]init];
[lineLayer setFrame:CGRectMake(x,y,width,1)];
[lineLayer setBackgroundColor:[UIColor whiteColor].CGColor];
[self.view.layer addSublayer:lineLayer];

Can't touch UIButton in UIScrollView

I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end

Dismiss custom alert view iOS

I'm setting up a subclass of UIView to roll my own UIAlertView style view. I've got everything set up properly with displaying the view, but I'm at a bit of a loss as to how to dismiss the view properly. Specifically, when a user taps on the button in the view, it needs to animate out of the main view. This is the code for the view itself:
+ (void)showCustomAlertWithTitle:(NSString *)titleString andMessage:(NSString *)messageString inView:(UIView *)view andButton1Title: (NSString *)button1Title andButton2Title: (NSString *)button2Title
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
CGRect windowFrame = window.frame;
[view setAlpha:0.5f];
UIColor *buttonColor = [UIColor colorWithRed:0/255.0f green:130/255.0f blue:216/255.0f alpha:1];
UIColor *titleColor = [UIColor colorWithRed:0/255.0f green:153/255.0f blue:102/255.0f alpha:1];
// Shade
UILabel *shadeWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height)];
shadeWindow.backgroundColor = [UIColor blackColor];
shadeWindow.alpha = 0.50f;
// Define size and origin of alert box
float alertBoxHeight = 225;
float alertBoxWidth = 200;
float alertBoxXorigin = windowFrame.size.width / 2 - (alertBoxWidth / 2);
float alertBoxYorigin = windowFrame.size.height / 2 - (alertBoxHeight / 2);
// Initialize background
UIView *alertBackground = [[UIView alloc] initWithFrame:CGRectMake(alertBoxXorigin, alertBoxYorigin, alertBoxWidth, alertBoxHeight)];
alertBackground.layer.cornerRadius = 5.0f;
[alertBackground.layer setMasksToBounds:YES];
alertBackground.layer.backgroundColor = [UIColor whiteColor].CGColor;
// Title Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertBoxWidth, 40)];
titleLabel.text = titleString;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = titleColor;
titleLabel.layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05f].CGColor;
[titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:20.0]];
// Title Divider
UILabel *titleDivider = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, 1.0)];
titleDivider.backgroundColor = [UIColor grayColor];
titleDivider.alpha = 0.5f;
// Alert Message Text
UITextView *alertMessage = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, alertBoxHeight - 90)];
alertMessage.text = messageString;
alertMessage.layer.backgroundColor = [UIColor whiteColor].CGColor;
[alertMessage setFont:[UIFont fontWithName:#"Avenir" size:15.0]];
alertMessage.textAlignment = NSTextAlignmentCenter;
alertMessage.textColor = [UIColor grayColor];
[alertMessage setEditable:NO];
// Button 1
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (button2Title == nil)
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, alertBoxWidth - 20, 40);
}
else
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
button2.frame = CGRectMake(alertBoxWidth / 2 + 10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
}
button1.layer.backgroundColor = buttonColor.CGColor;
[button1 setTitle:button1Title forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button1.layer.cornerRadius = 2.5f;
[button1.layer setMasksToBounds:YES];
// Button 2
button2.layer.backgroundColor = buttonColor.CGColor;
[button2 setTitle:button2Title forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button2.layer.cornerRadius = 2.5f;
[button2.layer setMasksToBounds:YES];
// Bounce Implementation
alertBackground.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
alertBackground.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
// do something once the animation finishes, put it here
}];
[window addSubview:view];
[window addSubview:alertBackground];
[view addSubview:shadeWindow];
[view bringSubviewToFront:alertBackground];
[alertBackground addSubview:button1];
[alertBackground addSubview:titleLabel];
[alertBackground addSubview:titleDivider];
[alertBackground addSubview:alertMessage];
[alertBackground addSubview:button2];
[alertBackground bringSubviewToFront:titleDivider];
/* [[[customAlerts sharedInstance] subViewArray] addObject:alertBackground];
[[[customAlerts sharedInstance] subViewArray] addObject:view];
[[[customAlerts sharedInstance] subViewArray] addObject:window];*/
}
When button1 is tapped, for instance, I need to have it animate the view out of the superView and remove it from the stack. I'm not sure how to handle this. Does anyone have any ideas?
To permanently remove a view, I generally use the UIView instance method removeFromSuperview. followed by a line setting the relevant variable to nil:
[myAlertView removeFromSuperview];
myAlertView = nil;
In your case then, I think you need to move the view off the screen by animating the its bounds property, then use the above couple of lines to remove any references to it.
Just found the answer here:
Dismiss view controller from #selector without creating seperate method
Had to download some custom classes but it worked:
[button1 addEventHandler:^(id sender, UIEvent *event)
{
[UIView animateWithDuration:0.25f animations:^{
[alertBackground setAlpha:0.0f];
[shadeWindow setAlpha:0.0f];
[window setAlpha:0.0f];
}];
} forControlEvent:UIControlEventTouchUpInside];
Custom classes can be found here:
https://github.com/ZeR0-Wu/JTTargetActionBlock

UITableViewController as a subview getting error

I have a UIViewController which is being loaded onto my UIViewController as a subview, however I am getting this error
Property 'frame' not found on object of type 'mynamedTableViewController *'
This is what my code looks like for the UIViewController.
.h
#import <UIKit/UIKit.h>
#import "FilterButtonsTableViewController.h"
#interface MainFilterViewController : UIViewController
#property (strong, nonatomic) UIButton *applyButton;
#property (strong, nonatomic) UIButton *cancelButton;
#property (strong, nonatomic) UIButton *clearAllButton;
#property (strong, nonatomic) FilterButtonsTableViewController *filterButtonsTableViewController;
#end
.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// add header label
UILabel *noteHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(25.0, 0.0, 200.0, 45.0)];
noteHeaderLabel.backgroundColor = [UIColor clearColor];
noteHeaderLabel.textColor = [UIColor lightGrayColor];
// dynamic using whatViewName
noteHeaderLabel.text = #"Filter";
[self.view addSubview:noteHeaderLabel];
// add underlines
// top
UIView *topUnderline = [[UIView alloc] initWithFrame:CGRectMake(0.0, 45.0, self.view.frame.size.width, 1.0)];
topUnderline.backgroundColor = [UIColor lightGrayColor];
topUnderline.alpha = 0.8;
// bottom
UIView *bottomUnderline = [[UIView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-45.0, self.view.frame.size.width, 1.0)];
bottomUnderline.backgroundColor = [UIColor lightGrayColor];
bottomUnderline.alpha = 0.8;
// addviews
[self.view addSubview:topUnderline];
[self.view addSubview:bottomUnderline];
// add buttons to view
// apply
applyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
applyButton.userInteractionEnabled = YES;
[applyButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchDown];
[applyButton setTitle:#"Save" forState:UIControlStateNormal];
applyButton.frame = CGRectMake(self.view.frame.size.width - 130, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:applyButton];
// cancel
cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.userInteractionEnabled = YES;
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
cancelButton.frame = CGRectMake(self.view.frame.size.width - 260, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:cancelButton];
// cancel
clearAllButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
clearAllButton.userInteractionEnabled = YES;
[clearAllButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[clearAllButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchUpInside];
[clearAllButton setTitle:#"Clear filter" forState:UIControlStateNormal];
clearAllButton.frame = CGRectMake(0.0, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:clearAllButton];
//TavleView
filterButtonsTableViewController = [[[FilterButtonsTableViewController alloc] init];
filterButtonsTableViewController.frame:CGRectMake(5.0, 65.0, (self.view.frame.size.width/2)-10, 235.0);
[self.view addSubview:filterButtonsTableViewController.view];
}
A viewcontroller does not have a frame property, only its view does.
Try this instead :
filterButtonsTableViewController.view.frame = CGRectMake(5.0, 65.0, (self.view.frame.size.width/2)-10, 235.0);
#interface MainFilterViewController : UIViewController
filterButtonsTableViewController = [[FilterButtonsTableViewController alloc] init];
[self.view addSubview:filterButtonsTableViewController.view];
// this code helps you to add tableview controller as a sub view.
#interface FilterButtonsTableViewController
self.view.frame=CGRectMake(0, 50, 150, 150);
// frame of the tableviewController is set on his class.

Resources