UIButton image does not show in Navigation Bar - ios

I am trying to a slide menu. The only problem is that the image for the UIButton does not show at runtime. This is the code I have written.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSString *)segueIdentifierForIndexPathInLeftMenu:(NSIndexPath *)indexPath
{
NSString *identifier;
switch (indexPath.row) {
case 0:
identifier=#"firstSegue";
break;
case 1:
identifier=#"secondSegue";
break;
}
return identifier;
}
-(void)configureLeftMenuButton:(UIButton *)button
{
CGRect frame = button.frame;
frame.origin=(CGPoint){0.0};
frame.size = (CGSize){40.40};
button.frame = frame;
[button setImage:[UIImage imageNamed:#"icon-menu"] forState:UIControlStateNormal];
}

I suppose you are using navigation controller. If so, implementation could look like:
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureLeftMenuButton];
}
-(void)configureLeftMenuButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"icon-menu"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
[button addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlStateNormal];
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = menuItem;
}

Related

UIButton selector not recognized by the following method

I have declared a method at the selector and I created a method with the same name. Yet, the method says it is not recognisable. The error messages:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIImage *btnImage = [UIImage imageNamed:#"ic_favorite_border_48pt.png"];
UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];
// get the image size and apply it to the button frame
CGRect listButtonFrame = heart.frame;
listButtonFrame.size = btnImage.size;
heart.frame = listButtonFrame;
[heart setImage:btnImage forState:UIControlStateNormal];
[heart addTarget:self.navigationController.parentViewController
action:#selector(revealToggle:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:heart];
self.navigationItem.rightBarButtonItem = jobsButton;
-(void)revealToggle:(UIButton *)heart
{
}
You have put the revealToggle method inside your viewDidAppear method. You can't do that. Move it outside.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIImage *btnImage = [UIImage imageNamed:#"ic_favorite_border_48pt.png"];
UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];
// get the image size and apply it to the button frame
CGRect listButtonFrame = heart.frame;
listButtonFrame.size = btnImage.size;
heart.frame = listButtonFrame;
[heart setImage:btnImage forState:UIControlStateNormal];
[heart addTarget:self
action:#selector(revealToggle:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:heart];
self.navigationItem.rightBarButtonItem = jobsButton;
}
-(void)revealToggle:(UIButton *)heart
{
}
And since revealToggle is inside this same class, you need to pass self as the target.

Programmatically Click 10 UIButtons one after another in UIScrollView

I am developing one control where 10 UIButtons are added in UIScrollView. Now I have a requirement to click every Button one after another after some delay. can you all guide me how to do that?
here is the code what I have done.
in viewcontroller.h file
#property (weak) IBOutlet UIScrollView *mapScrollView;
#property (strong) UIButton *addContactIcon;
in viewcontroller.m file
// Set up ScrollView with UIButtons
NSUInteger xPosition = 1;
NSUInteger countIndex = 0;
for (ContactModule *sortedContacts in _distanceList) {
_addContactIcon = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 7, 30, 30)];
_addContactIcon.tag = countIndex;
[_addContactIcon addTarget:self action:#selector(mapsScrollButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
_addContactIcon.layer.cornerRadius = 2.0;
_addContactIcon.clipsToBounds = YES;
[_addContactIcon setBackgroundImage:[UIImage imageWithContentsOfFile:dataPath] forState:UIControlStateNormal];
[_addContactIcon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_mapScrollView addSubview:_addContactIcon];
xPosition += _addContactIcon.frame.size.width + 2;
countIndex = countIndex + 1;
}
_mapScrollView.contentSize = CGSizeMake(30 * _distanceList.count + 34, 40);
[_mapScrollView setShowsHorizontalScrollIndicator:NO];
Here is the Button Click Method For Every Button:
- (void)mapsScrollButtonClicked:(UIButton *)clickButton {
// Set Annotation Callout
[_mapsView selectAnnotation:[_mapsView.annotations objectAtIndex:clickButton.tag] animated:YES];
}
Now the requirement is I want to call Action Method for Every UIButtons in UIScrollview. for that I am doing something wrong below. help me with that:
for(NSUInteger count=0; count<[_distanceList count];count++) {
[UIView animateWithDuration:0.4
delay:0.8
options:0
animations:^{
[_addContactIcon sendActionsForControlEvents:UIControlEventTouchUpInside];
} completion:nil];
}
Hi you could do soemthing like below to click all the UIButtons on your view.
UIButton *button;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *buttonMaster;
buttonMaster = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonMaster addTarget:self
action:#selector(masterButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[buttonMaster setTitle:#"Show View" forState:UIControlStateNormal];
buttonMaster.frame = CGRectMake(80.0, y, 160.0, 40.0);
buttonMaster.tag = 100;
[self.view addSubview:buttonMaster];
y = 60;
for (int x=0; x<=10; x++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(button1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, y, 160.0, 30.0);
button.tag = x;
//[paintView addSubview:button];
[self.view addSubview:button];
y = y + 70;
}
[buttonMaster sendActionsForControlEvents:UIControlEventTouchUpInside];
if ([[self.view viewWithTag:0] isKindOfClass:[UIButton class]]) {
//UIButton *button = (UIButton *) view;
// do something funky with button
UIButton *myBtns = (UIButton *)[self.view viewWithTag:0];
NSLog(#" button tapped is %ld", myBtns.tag);
[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
[myBtns setBackgroundColor:[UIColor greenColor]];
} else {
// do something funky with view
NSLog(#"do nothing ");
}
}
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
//if ([subviews count] == 0) return; // COUNT CHECK LINE
for (UIView *subview in subviews) {
UIButton *myBtns = (UIButton *)[self.view viewWithTag:subview.tag];
//UIButton *myBtns = (UIButton *)[self.view viewWithTag:0];
if(myBtns.tag == 100 ){
NSLog(#"Master Button tag :- Dont do anything ");
}else
{
//[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
NSLog(#"tags are %ld ", myBtns.tag);
}
// List the subviews of subview
//[self listSubviewsOfView:subview];
}
}
-(void) clickAllButtons{
for (int i = 0; i<=10; i++) {
UIButton *myBtns = (UIButton *)[self.view viewWithTag:i];
[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (IBAction)masterButtonTapped:(UIButton*)sender{
//[self listSubviewsOfView:self.view];
// [self clickAllButtons];
}
- (IBAction)button1:(UIButton*)sender{
NSLog(#"test");
//NSLog(#"Button clicked %ld", tag);
}

how to create BOOL property for UIButton for checked selecting

I create many button with loop from array but I want when click on any button I could check select state this button with one bool property with name : Selected.
when any button that I click on it selected change background to red color and when deselected change background to blue color.
please guide me how to create one property for any button.
this is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSMutableArray *arrayString = [[NSMutableArray alloc]initWithObjects:#"Mohammad",#"Hamidreza",#"Ahmad",#"Amirhossein",#"javad",#"asdasddasdsdasd", nil];
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:20];
CGFloat x = 5;
CGFloat y = 80;
for (NSString *text in arrayString) {
UIButton *button = [[UIButton alloc]init];
CGFloat width = [self widthOfString:text withFont:myFont];
CGFloat height = [self heightOfString:text withFont:myFont];
if ((x + width) > 320) {
NSLog(#"after line");
x = 5;
y += (height+20)+10;
button.frame = CGRectMake(x, y, width+20, height+20);
[button setTitle:text forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
x += (width+20)+10;
}
else {
NSLog(#"this line");
button.frame = CGRectMake(x, y, width+20, height+20);
[button setTitle:text forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
x += (width+20)+10;
}
[button addTarget:self action:#selector(Selected:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(IBAction)Selected:(id)sender{
if (//check sender button property bool selected) {
}
else {
}
}
Try this code:-
- (void)viewDidLoad
{
[super viewDidLoad];
[self.button setBackgroundImage:[self imageWithColor:[UIColor redColor]]forState:(UIControlStateHighlighted)];
[self.button setBackgroundImage:[self imageWithColor:[UIColor redColor]]forState:(UIControlStateSelected)];
[self.button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:(UIControlStateNormal)];
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Please, name your method starting with a lowercase. That's not related to your issue, but that's convention.
Since your question is more about "How do I get the button that was touched ?", than actually do your thing:
-(IBAction)selected:(id)sender //may rename this method to, it's quite strange, look like a "state/getter"
{
UIButton *button = (UIButton *)sender;
//Do something
if ([[button backgroundColor] isEqual:[selectedColor]])
{
//Do something
}
else
{
//Do something
}
}
my dude try this :
you don't need to create property for UIButton for this. you can do it with this code:
-(IBAction)Selected:(id)sender{
UIButton *button = (UIButton *)sender;
if (!button.selected) {
button.backgroundColor = [UIColor blueColor];
button.selected = YES;
}
else{
button.backgroundColor = [UIColor redColor];
button.selected = NO;
}
}
this code working for any button that you created!!! :D

PFQueryTableView not Updating/How to Update

new day new Problem I guess.
I'm using JASidePanel with 5 Buttons in it,each button Changes the Value of a Variable.
The Variable is used in MyTableController.m as parseClassName.
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom the table
// The className to query on
if (_sDay==Nil) {
self.parseClassName = #"Montag";
}else{
self.parseClassName = self.sDay;
}
// The key of the PFObject to display in the label of the default cell style
self.textKey = #"Fach";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
// The number of objects to show per page
self.objectsPerPage = 5;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hintergrund"]];
imageView .frame = CGRectMake(0,0,255,458);
[self.view addSubview:imageView];
CGRect frameBtn = CGRectMake(0.0f, 0.0f, 254.0f, 50.0f);
self.bMonday = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bMonday setBackgroundImage:[UIImage imageNamed:#"button_rechts_1"] forState:UIControlStateNormal];
[self.bMonday setTitle:#"Montag" forState:UIControlStateNormal];
[self.bMonday setFrame:frameBtn];
[self.bMonday addTarget:self
action:#selector(monday:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.bMonday];
CGRect frameBtn2 = CGRectMake(0.0f, 50.0f, 254.0f, 50.0f);
self.bTuesday = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bTuesday setBackgroundImage:[UIImage imageNamed:#"button_rechts_1"] forState:UIControlStateNormal];
[self.bTuesday setTitle:#"Dienstag" forState:UIControlStateNormal];
[self.bTuesday setFrame:frameBtn2];
[self.bTuesday addTarget:self
action:#selector(tuesday:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.bTuesday];
CGRect frameBtn3 = CGRectMake(0.0f, 100.0f, 254.0f, 50.0f);
self.bWendsday = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bWendsday setBackgroundImage:[UIImage imageNamed:#"button_rechts_1"] forState:UIControlStateNormal];
[self.bWendsday setTitle:#"Mittwoch" forState:UIControlStateNormal];
[self.bWendsday setFrame:frameBtn3];
[self.bWendsday addTarget:self
action:#selector(wendsday:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.bWendsday];
CGRect frameBtn4 = CGRectMake(0.0f, 150.0f, 254.0f, 50.0f);
self.bThursday = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bThursday setBackgroundImage:[UIImage imageNamed:#"button_rechts_1"] forState:UIControlStateNormal];
[self.bThursday setTitle:#"Donnerstag" forState:UIControlStateNormal];
[self.bThursday setFrame:frameBtn4];
[self.bThursday addTarget:self
action:#selector(thursday:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.bThursday];
CGRect frameBtn5 = CGRectMake(0.0f, 200.0f, 254.0f, 50.0f);
self.bFriday = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bFriday setBackgroundImage:[UIImage imageNamed:#"button_rechts_1"] forState:UIControlStateNormal];
[self.bFriday setTitle:#"Freitag" forState:UIControlStateNormal];
[self.bFriday setFrame:frameBtn5];
[self.bFriday addTarget:self
action:#selector(friday:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.bFriday];
}
-(void)monday:(id)sender {
UINavigationController *navController = (UINavigationController*) self.sidePanelController.centerPanel;
MyTableController *myNewTableController = (MyTableController*)[navController.viewControllers objectAtIndex:0];
myNewTableController.sDay = #"Montag";
NSLog(#"%#",myNewTableController.sDay);
}
-(void)tuesday:(id)sender {
UINavigationController *navController = (UINavigationController*) self.sidePanelController.centerPanel;
MyTableController *myNewTableController = (MyTableController*)[navController.viewControllers objectAtIndex:0];
myNewTableController.sDay = #"Dienstag";
NSLog(#"%#",myNewTableController.sDay);
}
-(void)wendsday:(id)sender {
UINavigationController *navController = (UINavigationController*) self.sidePanelController.centerPanel;
MyTableController *myNewTableController = (MyTableController*)[navController.viewControllers objectAtIndex:0];
myNewTableController.sDay = #"Mittwoch";
NSLog(#"%#",myNewTableController.sDay);
}
-(void)thursday:(id)sender {
UINavigationController *navController = (UINavigationController*) self.sidePanelController.centerPanel;
MyTableController *myNewTableController = (MyTableController*)[navController.viewControllers objectAtIndex:0];
myNewTableController.sDay = #"Donnerstag";
NSLog(#"%#",myNewTableController.sDay);
}
-(void)friday:(id)sender {
UINavigationController *navController = (UINavigationController*) self.sidePanelController.centerPanel;
MyTableController *myNewTableController = (MyTableController*)[navController.viewControllers objectAtIndex:0];
myNewTableController.sDay = #"Freitag";
NSLog(#"%#",myNewTableController.sDay);
}
The Problem now is that the PFQueryUITableView isn't displaying the content of the new parseClass, does anybody have an Idea why?
Thanks alot in advance!
In your monday ... friday methods, you're setting sDay but you're not changing the value stored in parseClassName. One way you could fix it would be to add the following to each of your methods
myNewTableController.parseClassName = #"SOME VALUE";
Another option would be to customize the setter for sDay so that it updates parseClassName the code would look like the following
- (void)setSDay:(NSString *)sDay
{
if (![_sDay isEqualToString:sDay]) {
_sDay = sDay;
self.parseClassName = sDay;
}
}

Programmatically created button doesn't response to user's click

I know that this kind of issue discussed several times here but, I have already searched and tried without success. I am trying to create a simple login view on the top of general menu. This login page contains 2 textfields(username & password) and 1 button(login). My problem is that while everything appears perfectly when I click on Login button, is not responding.
*The login view animated from start to end point using moveTo method. Comment that part but still not responding.
Main.m
- (void)viewDidLoad
{
prefs = [NSUserDefaults standardUserDefaults];
if (![prefs objectForKey:#"userName"]) {
LoginScreen *login = [[LoginScreen alloc] initWithFrame:CGRectMake(35, 400, 250, 350)];
[self.view addSubview:login];
[login moveTo:CGPointMake(35.0, 65.0) duration:0.6 option:UIViewAnimationOptionCurveEaseOut];
}
[super viewDidLoad];
}
LoginScreen.m
#synthesize login;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.masksToBounds = YES;
self.userInteractionEnabled = YES;
[self addButton:login withTitle:#"Login" andSize:CGRectMake(85, 200, 90, 35)];
[login addTarget:self action:#selector(attemptLogin)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void) addButton: (UIButton*) button withTitle: (NSString*) title andSize: (CGRect) size{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = size;
[button setTitle:title forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[self addSubview:button];
}
- (void)attemptLogin{
NSString *user = usernameTxt.text;
NSString *pass = passwordTxt.text;
NSString *url = [[NSString alloc] initWithFormat:#"http://domain.com/login.php?username=%#&password=%#", user, pass];
NSLog(#"%#", url);
}
Nothing print on screen.
Thanks in advance
The problem is mainly you are allocting a new button, therefore not working with your login button, I commented the allocation on your code and this should work:
//Old version of code removed, please check edit
Edit
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.masksToBounds = YES;
self.userInteractionEnabled = YES;
login = [self addButtonWithTitle:#"Login" andSize:CGRectMake(85, 200, 90, 35)];
[login addTarget:self action:#selector(attemptLogin)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (UIButton) addButtonWithTitle: (NSString*) title andSize: (CGRect) size{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = size;
[button setTitle:title forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[self addSubview:button];
return button;
}
- (void)attemptLogin{
NSString *user = usernameTxt.text;
NSString *pass = passwordTxt.text;
NSString *url = [[NSString alloc] initWithFormat:#"http://domain.com/login.php?username=%#&password=%#", user, pass];
NSLog(#"%#", url);
}
This is because you created a button without specifying what selector to call when the button is clicked. Adding this line to your addButton method will fix the problem:
[button addTarget:self action:#selector(attemptLogin) forControlEvents:UIControlEventTouchUpInside];
When you try doing the same through the synthesized login property, the code does not work, because you never set login. When you pass it to the addButton function, the value gets ignored (you re-assign it right away). The assignment never changes the value of login, though, because Objective C passes parameters by value.
An alternative way to fix your code is to pass a pointer to login, rather than login itself, like this:
- (void) addButton: (UIButton**) button withTitle: (NSString*) title andSize: (CGRect) size {
*button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
(*button).frame = size;
[*button setTitle:title forState:UIControlStateNormal];
(*button).userInteractionEnabled = YES;
[self addSubview:*button];
}
I would recommend against this way of fixing your code: using login directly inside addButton would probably be a better choice.
I think that you meant:
- (void) addButton: (UIButton*) button withTitle: (NSString*) title andSize: (CGRect) size{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = size;
[button setTitle:title forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(attemptLogin)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
You should also remove that:
[login addTarget:self action:#selector(attemptLogin)
forControlEvents:UIControlEventTouchUpInside];
from the init method.

Resources