UIButton not changing anything with #selector - new Issue - ios

Need help. I am trying to change an image of an ImageView using a programmed button. I copied some of the sample codes out here. The sitting on highlighted button works but not doing anything else when clicked. Thanks.
Thanks for the replies. I managed to change the photos but created more problem. I believe it is clear that that I am new to Xcode.
New problems:
1. The new image stays on top so i cannot access the button.
2. I would like to have the image changed only when the button is highlighted.
3. With the new image, it created warning: "Hides instance variable" on the relayImages properties. Thanks again.
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
#synthesize relayImages;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self addMyImages];
[self addMyButton];
[self addMyLabel];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)addMyButton
{
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 20, 78)];
UIImage *offState = [UIImage imageNamed:#"PBNoOff.png"];
[playButton setBackgroundImage:offState forState:UIControlStateNormal];
UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[playButton setBackgroundImage:onState forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"1_2RelaysOn.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
[self.view addSubview:_playButton];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"1_2RelaysOff.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyLabel
{
UILabel *checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 30)];
checkStatusLabel.backgroundColor = [UIColor greenColor];
checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:checkStatusLabel];
}

try this i am created custom imageView
.m file
-(void)addMyButton
{
_playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 60, 50)];
[_playButton setTitle:#"button" forState:UIControlStateNormal];
[_playButton setBackgroundColor:[UIColor grayColor]];
UIImage *offState = [UIImage imageNamed:#"1.jpg"];
// [playButton setBackgroundImage:offState forState:UIControlStateNormal];
// UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[_playButton setBackgroundImage:offState forState:UIControlStateHighlighted];
[_playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"2.jpg"];
relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(20, 100, 100, 100);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"3.jpg"];
UIImageView *someImages = [[UIImageView alloc] initWithImage:image];
someImages.frame = CGRectMake(20, 200, 100, 100);
someImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:someImages];
}
-(void)addMyLabel
{
_checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100, 30)];
_checkStatusLabel.backgroundColor = [UIColor greenColor];
_checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:_checkStatusLabel];
}
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
and viewdidload
[self addMyButton];
[self addMyImages];
[self addMyLabel];

Related

didTapInfoWindowOfMarker multiple buttons click event info

I am displaying some map points in google map. when user will click on that map point. I markerInfoWindow will be display. In this window, I am using two buttons. whenever user will click on that buttons. didTapInfoWindowOfMarker fires. but i amn't able to identify that which one has been clicked. may you please help me for that? i don't want to use any other approach.
Thanks
update:- This is infoWindow code.
-(void)prepareBubble
{
// UIView *bubble = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
self .frame = CGRectMake(0, 0, 260, 130);
self.backgroundColor = [UIColor clearColor];
// self.layer.borderColor = [UIColor grayColor].CGColor;
// self.layer.borderWidth = 1;
// self.layer.cornerRadius = 7;
UIImageView *bg = [[UIImageView alloc] initWithFrame:self.frame];
bg.image = [UIImage imageNamed:#"popup.png"];
[self addSubview:bg];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 5, 150, 40)];
self.titleLabel.backgroundColor = [UIColor clearColor];
[self.titleLabel setAdjustsFontSizeToFitWidth:TRUE];
self.titleLabel.numberOfLines = 0;
self.titleLabel.font = [UIFont fontWithName:FontNameArialBold size:14];
self.titleLabel.text = _title;
self.addressLabel= [[UILabel alloc] initWithFrame:CGRectMake(90, 40, 150, 35)];
self.addressLabel.backgroundColor = [UIColor clearColor];
[self.addressLabel setAdjustsFontSizeToFitWidth:TRUE];
self.addressLabel.numberOfLines = 0;
self.addressLabel.font = [UIFont fontWithName:FontNameArial size:12];
self.addressLabel.text = _address;
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 70, 70)];
[self addSubview:self.imageView];
self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl]]];
if ( self.imageView.image == nil )
{
self.imageView.image = [UIImage imageNamed:DefaultImage100];
}
// [[[CustomNetwork alloc] init] setImage:_imageUrl onImageView:self.imageView withPlaceHolderImage:DefaultImage100];
[self addSubview:self.titleLabel];
[self addSubview:self.addressLabel];
if ( _ratings > 0 )
{
[self addSubview:[self addStar:_ratings]];
}
if( _hasTeeTimes ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Book" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"button_orange_large.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(200, 75, 45, 25);
[self addSubview:button];
}
// return self;
}
here I am trying to make to click listener, first is on button and another is on tap in any part of this window like as textField etc. so my two new view will push
Change the buttons to properties.
Give the buttons tags.
Logically
determine by identifying the sender and see if it is > or < the other
button (which you need to keep in memory by making them properties or
instance variables).
load custom Xibs, and all of these will work with 0 lines of code.

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.

Not called button after adding in to container

I have container view, where I add some image and button.
Container view is property of UIView.
I'll add my Container View to ScrollView like this:
#property (nonatomic,strong) UIView *containerView;
..
containerView = [[UIView alloc] init];
for (int i=0;i<5;i++) {
UIImageView *whiteBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 235-height, 385-height-height)];
whiteBack.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-131+height);
whiteBack.backgroundColor = [UIColor darkGrayColor];
whiteBack.layer.cornerRadius = 3;
whiteBack.layer.masksToBounds = YES;
[containerView addSubview:whiteBack];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 228-height, 340-height-height)];
img.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-150+height);
img.image = [UIImage imageNamed:[NSString stringWithFormat:#"poster0%i.jpg",i+1]];
[containerView addSubview:img];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(233, 0, 25, 25);
infoButton.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2-100, 325-height);
[infoButton addTarget:self action:#selector(flipImage:) forControlEvents:UIControlEventTouchUpInside];
infoButton.tag = i;
infoButton.backgroundColor = [UIColor clearColor];
[infoButton setImage:[UIImage imageNamed:#"upload-50"] forState:UIControlStateNormal];
[infoButton setTintColor:[UIColor whiteColor]];
[containerView addSubview:infoButton];
}
[self.scrollView addSubview:containerView];
///
- (void) flipImage :(id) sender {
NSLog(#"infoButton pressed:%i",[sender tag]);
}
but when I pressed button nothing happens!
Your container view has a zero frame (because you just did [[UIView alloc] init];). This means that it never receives any touches because you can never hit it. This goes for the subviews too.
The reason you see the subviews is that views, by default, do not clip drawing to their bounds.
To fix, set the frame of your container view to an appropriate size for the subviews you're adding.

Showing UIButton Done on UIScrollview for images

Want to show UIButton done on UIScrollView for images. With the below code UIButton is showing but when I scroll UIImageViews, it shows only on very first imageview where as I want UIButton done should show on all image views in UIScrollView.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
How should I code it that UIButton Done should show on all image views when I scroll it.
Add your UIButton in for loop i.e. it can add each time when your new ImageView is add.
And change its x_Origin for each imageView -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
}
Remove these 2 lines
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
There is no need to do this. This means you're setting that button on rightBarButton of NavigationBar but in your case i think you dont want to do so.
Set frame for myButton in for loop similar to imageview frame.
myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

Resources