-(void)setButtons_AsPerTheMatrixSelection
{
for(UIView *subview in [viewWithButtons subviews])
{
if ([subview isKindOfClass:[UIButton class]])
{
[subview removeFromSuperview];
}
}
viewWithButtons = [[UIView alloc] init];
width = 48;
height = 48;
pw = 49;
ph = 49;
arrButton = [[NSMutableArray alloc] init];
UIImage *imgDefaultBG = [UIImage imageNamed:#"bg.jpg"];
viewWithButtons.frame = CGRectMake(50, 40, 200, 260);
ch = 4;
cv = 4;
for ( i = 0 ; i < cv ; ++i )
{
for ( j = 0 ; j < ch ; ++j )
{
btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(10+pw*j, 51+ph*i, width, height)] autorelease];
btnMatrix.tag = i*ch+j;
btnMatrix.userInteractionEnabled = TRUE;
bulImageStatus = FALSE;
[btnMatrix addTarget:self action:#selector(changeImage:) forControlEvents:UIControlEventTouchDown];
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
[viewWithButtons addSubview:btnMatrix];
[arrButton addObject:btnMatrix];
}
}
NSLog(#"arrButton object count is:--> %d",[arrButton count]);
[self.view addSubview:viewWithButtons];
}
-(void)AddImageToArray
{
arr_FirstSet = [[NSMutableArray alloc] init];
NSString *strImageName;
if(appDelegate.intCategoryBtnTag == 0)
{
for (intimg = 1; intimg <= 28; intimg++)
{
strImageName = [NSString stringWithFormat:#"%d_1.png",intimg];
NSLog(#"strImageName is :--> %#",strImageName);
[arr_FirstSet addObject:strImageName];
}
NSLog(#"arr_FirstSet objects are...%#",arr_FirstSet);
}
}
-(void)changeImage:(id)sender
{
UIImage *img;
NSString *strImageName;
strImageName = [arr_FirstSet objectAtIndex:arc4random() % [arr_FirstSet count]/2];
NSLog(#"btnMatrix is:--> %#",strImageName);
img = [UIImage imageNamed:strImageName];
//[btnMatrix setImage:img forState:UIControlEventTouchDown];
NSLog(#"sender detail is:--> %#",sender);
[sender setBackgroundImage:img forState:UIControlStateHighlighted];
}
This is my code to set the dynamic buttons in "setButtons_AsPerTheMatrixSelection" method,
"AddImageToArray" method is used to add images from bundle to NSMutableArray (arr_FirstSet) one by one.
"changeImage" method is used to set the background of particular button.
I am able to set the images as background to the buttons randomly,
But the main problem is that i have to set fixed dynamic image to particular button.
right now on each click of particular button i am getting changed random image when i press it once, twice, thrice etc...
I have to set any particular image which is generated randomly in the "changeImage" to single button & rest to the other buttons single-single.
Then i have to check if two buttons have same background then these two buttons will be removed from the matrix.
Please guide me for the mistake i am doing & help me.
//Please replace this method
-(void)changeImage:(UIButton *)sender
{
UIImage *img;
NSString *strImageName;
strImageName = [arr_FirstSet objectAtIndex:arc4random() % [arr_FirstSet count]/2];
img = [UIImage imageNamed:strImageName];
//for the first time sender.imageView.image property will be null then only we set image to the button.
//For second click this condition fails and does not set the other image.
if(!sender.imageView.image)
{
[sender setImage:img forState:UIControlStateHighlighted];
[sender setImage:img forState:UIControlStateSelected];
}
// Calling a method to check if two images of buttons are same.
[self checkIfTwoImagesAreSame:sender];
}
//And Add this method
- (void)checkIfTwoImagesAreSameImageMaching:(UIButton *)sender
{
for(UIView *subview in [viewWithButtons subviews])
{
if ([subview isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)subview;
// This condition is comparing clicked button image with all the buttons images of the viewWithButtons view if it isEqual:
if(sender.imageView.image && btn.imageView.image && [sender.imageView.image isEqual:btn.imageView.image])
{
// this is checking if the clicked button is not comparing with itself
if(btn.tag != sender.tag)
{
[btn removeFromSuperview];
[sender removeFromSuperview];
// OR
// btn.selected = YES;
// sender.selected = YES;
// OR
// btn.hidden = YES;
// sender.hidden = YES;
}
}
}
}
}
Related
I have some generic generated ImageViews in a scrollView each of these images have 2 gestureRecognizer for single/double tapping on the ImageView. Now my problem is how to identify whether the ImageView was tapped for first or second time. In some scenarios it's easy to use the tag of an ImageView, but I have two different gestureRecognizer on each ImageView and every gestureRecognizer uses a different identification method based on the tag number, to identify the image.
Here I generate the ImageViews dynamically:
-(void) initLevels{
_level = [Level alloc];
_unit = [Unit alloc];
self->_units = [[NSMutableArray alloc] init];
_keys = [[NSMutableArray alloc] init]
;
int x = 0;
int y = 0;
int i = 0;
for (NSObject *object in self->_levels) {
if ([object isKindOfClass:_level.class] && i != 0) {
x = x + MARGIN_RIGHT + OBJECT_WIDTH;
y = 0;
}
else if ([object isKindOfClass:_unit.class]){
_unit = (Unit *) object;
[self->_units addObject:_unit.description];
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.box];
[imageView setFrame:CGRectMake(x, y, OBJECT_WIDTH, BOX_HEIGHT)];
imageView.highlighted = TRUE;
imageView.tag = i; //when this is not outlined the gestureRecognizer for singleTapping works but on the other hand the double tap gestureRecognizer just works for the first object, because its' tag is set on 0.
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(unitDoubleTapped:)];
doubleTapGesture.numberOfTapsRequired = 2;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:doubleTapGesture];
UITapGestureRecognizer *singleTapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(unitSingleTapped:)];
singleTapGesture.numberOfTapsRequired = 1;
//telling the singleTapGesture to fail the doubleTapGesture, so both doesn't fire at the same time
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
[imageView addGestureRecognizer:singleTapGesture];
UILabel *labelHeadline = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 220, 20)];
[labelHeadline setFont:[UIFont boldSystemFontOfSize:12]];
labelHeadline.textAlignment = NSTextAlignmentCenter;
[labelHeadline setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0]];
labelHeadline.text = _unit.headline;
labelHeadline.numberOfLines = 0;
[labelHeadline sizeToFit];
UILabel *labelPrice = [LabelUtils deepLabelCopy:labelHeadline withText:[NSString stringWithFormat:#"Price: %#",_unit.price] withFrame:NO];
[labelPrice setTextAlignment:NSTextAlignmentLeft];
[labelPrice setFrame:CGRectMake(labelHeadline.frame.origin.x, labelHeadline.frame.origin.y + labelHeadline.frame.size.height + 2, 220, 20)];
UILabel *labelCRM = [LabelUtils deepLabelCopy:labelHeadline withText:[NSString stringWithFormat:#"CRM: %#", _unit.crm] withFrame:NO];
[labelCRM setTextAlignment:NSTextAlignmentLeft];
[labelCRM setFrame:CGRectMake(labelPrice.frame.origin.x, labelPrice.frame.origin.y + labelPrice.frame.size.height + 2, 220, 20)];
UITextView *textView= [[UITextView alloc] initWithFrame:CGRectMake(0,0, OBJECT_WIDTH, OBJECT_HEIGHT)];
[textView addSubview:labelHeadline];
[textView addSubview:labelPrice];
[textView addSubview:labelCRM];
[textView setUserInteractionEnabled:NO];
[textView setEditable:NO];
textView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
textView.textAlignment = NSTextAlignmentLeft;
[imageView addSubview:textView];
[_scrollView addSubview:imageView];
y = y + MARGIN_BOTTOM + BOX_HEIGHT;
}
[self->_keys addObject:[NSNumber numberWithInt:i]];
i++;
}//remove the last keys which are to much in the _keys array
while ([self->_keys count] > ([self->_units count])) {
[_keys removeLastObject];
i--;
}
self.contents = [NSDictionary dictionaryWithObjects:self->_units forKeys:_keys];
}
Here is the code for the two gesture Recognizer
-(void)unitDoubleTapped:(UIGestureRecognizer *)gestureRecognizer{
self->_unitViewForDoubleTapIdentification = (UIImageView *)gestureRecognizer.view;
switch (self->_unitViewForDoubleTapIdentification.tag) {
case 0:
[_unitViewForDoubleTapIdentification setHighlightedImage:self.transparentBox];
self->_unitViewForDoubleTapIdentification.tag = 1;
break;
case 1:
[_unitViewForDoubleTapIdentification setHighlightedImage:self.box];
self->_unitViewForDoubleTapIdentification.tag = 0;
break;
default:
break;
}
}
and Here the singleTap
- (IBAction)unitSingleTapped:(id)sender {
[self dismissAllPopTipViews];
UIGestureRecognizer *gestureRecognizer = [UIGestureRecognizer alloc];
gestureRecognizer = (UIGestureRecognizer *)sender;
UIImageView *imageView = [[UIImageView alloc] init];
imageView = (UIImageView *)gestureRecognizer.view;
if (sender == _currentPopTipViewTarget) {
// Dismiss the popTipView and that is all
self.currentPopTipViewTarget = nil;
}
NSString *contentMessage = nil;
UIImageView *contentView = nil;
NSNumber *key = [NSNumber numberWithInt:imageView.tag];
id content = [self.contents objectForKey:key];
if ([content isKindOfClass:[NSString class]]) {
contentMessage = content;
}
else {
contentMessage = #"A large amount ot text in this bubble\najshdjashdkgsadfhadshgfhadsgfkasgfdasfdhasdkfgaodslfgkashjdfg\nsjfkasdfgkahdjsfghajdsfgjakdsfgjjakdsfjgjhaskdfjadsfgjdsfahsdafhjajdskfhadshfadsjfhadsjlfkaldsfhfldsa\ndsfgahdsfgajskdfgkafd";
}
NSArray *colorScheme = [_colorSchemes objectAtIndex:foo4random()*[_colorSchemes count]];
UIColor *backgroundColor = [colorScheme objectAtIndex:0];
UIColor *textColor = [colorScheme objectAtIndex:1];
CMPopTipView *popTipView;
if (contentView) {
popTipView = [[CMPopTipView alloc] initWithCustomView:contentView];
}
else {
popTipView = [[CMPopTipView alloc] initWithMessage:contentMessage];
}
[popTipView presentPointingAtView:imageView inView:self.view animated:YES];
popTipView.delegate = self;
popTipView.disableTapToDismiss = YES;
popTipView.preferredPointDirection = PointDirectionUp;
if (backgroundColor && ![backgroundColor isEqual:[NSNull null]]) {
popTipView.backgroundColor = backgroundColor;
}
if (textColor && ![textColor isEqual:[NSNull null]]) {
popTipView.textColor = textColor;
}
popTipView.animation = arc4random() % 2;
popTipView.has3DStyle = (BOOL)(arc4random() % 2);
popTipView.dismissTapAnywhere = YES;
[popTipView autoDismissAnimated:YES atTimeInterval:3.0];
[_visiblePopTipViews addObject:popTipView];
self.currentPopTipViewTarget = sender;
}
Hope you can help me, thanks in advance.
that's a lot of code to go through.. but I add more identifiers to UIViews (ie other than the tag identifier) by using obj-c runtime associative references..
So this is a category I attach to my UIView:
#import "UIView+Addons.h"
#import <objc/runtime.h>
#define kAnimationDuration 0.25f
#implementation UIView (Addons)
static char infoKey;
static char secondaryInfoKey;
-(id)info {
return objc_getAssociatedObject(self, &infoKey);
}
-(void)setInfo:(id)info {
objc_setAssociatedObject(self, &infoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(id)secondaryInfo {
return objc_getAssociatedObject(self, &secondaryInfoKey);
}
-(void)setSecondaryInfo:(id)info {
objc_setAssociatedObject(self, &secondaryInfoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#end
here is an example of my own code where I use the above to address a problem similar to the one you're facing:
if (![[view info] boolValue]) { // not selected?
self.moveToFolderNumber = [view secondaryInfo];
[view setInfo:#YES];
}
In one of the projects I'm working on, I have a UIScrollView that has a lot of buttons and labels that resemble movie theater seats that the user can tap to select seats for them to buy. Is it possible to zoom the UIScrollView? I have already declared the UIScrollViewDelegate, set the delegate to self, set the minimum and maximum zoom scale but I'm having issues with the viewForZoomingInScrollView and scrollViewDidEndZooming methods. I tried to return my scrollview on the viewForZoomingInScrollView method but it crashes.
Here's some of the code:
- (void) renderSeatMapOnScreen
{
int x_offset_row = 10;
int y_offset_row = 25;
int x_offset_col = 30;
int y_offset_col = 0;
int scrollview_w = 0;
int scrollview_h = 0;
NSString *row_name = #"";
int tag_index = 0;
NSMutableArray *seat_map_table = [seat_map_xml seats_map];
NSString *seat_available_str = [seat_avail_xml seat_available];
HideNetworkActivityIndicator();
NSLog(#"Seats available = %#", seat_available_str);
NSArray *seats_avail = [seat_available_str componentsSeparatedByString:#";"];
int ROW_COL_OFFSET = 25;
for (int row = 0; row < [seat_map_table count]; row++)
{
UILabel * rowlabel = [[UILabel alloc] init];
rowlabel.frame = CGRectMake(x_offset_row , y_offset_row, 22, 22);
SeatMapDeclaration *rowmap = [seat_map_table objectAtIndex:row];
rowlabel.text = rowmap.rows;
[scrollview addSubview:rowlabel];
row_name = [NSString stringWithFormat:#"%#", rowmap.rows];
NSArray *seat = [rowmap.rowmap componentsSeparatedByString:#";"];
NSLog(#"row[%i] = %#", row, rowmap.rowmap);
if (row == 0)
{
scrollview_w = [seat count] * ROW_COL_OFFSET + y_offset_row;
total_column = [seat count];
total_row = [seat_map_table count];
}
x_offset_col = 30 ;
y_offset_col = y_offset_row ;
for (int column = 0; column < [seat count]; column++)
{
if (([[seat objectAtIndex:column] rangeOfString:#"a("].location != NSNotFound)
|| ([[seat objectAtIndex:column] isEqualToString:#""]))
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView setImage:[UIImage imageNamed:#"noseat.png"]];
[scrollview addSubview:imageView];
}
else if ([[seat objectAtIndex:column] rangeOfString:#"b("].location != NSNotFound)
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
imageView.image = [UIImage imageNamed:#"noseat.png"];
[scrollview addSubview:imageView];
}
else
{
NSString *status = [NSString stringWithFormat:#"%#%#", row_name, [seat objectAtIndex:column]];
BOOL matchedFound = NO;
for (int i = 0; i < [seats_avail count]; i++)
{
if ([[seats_avail objectAtIndex:i] isEqualToString:status])
{
matchedFound = YES ;
break ;
}
}
if (matchedFound == YES)
{
UIButton * seat_btn = [UIButton buttonWithType:UIButtonTypeCustom];
[seat_btn setBackgroundImage:[UIImage imageNamed:#"seatavailable.png"] forState:UIControlStateNormal];
[seat_btn setBackgroundImage:[UIImage imageNamed:#"seatactive.png"] forState:UIControlStateSelected];
seat_btn.frame = CGRectMake(x_offset_col, y_offset_col, 22, 22);
tag_index = [[seat objectAtIndex:column] intValue];
[seat_btn setTitle:[seat objectAtIndex:column] forState:UIControlStateNormal];
seat_btn.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12.0];
seat_btn.tag = tag_index + row * 100;
[seat_btn addTarget:self
action:#selector(checkboxButton:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:seat_btn];
}
else
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
imageView.image = [UIImage imageNamed:#"seat_not_available.png"];
[scrollview addSubview:imageView];
}
}
x_offset_col += ROW_COL_OFFSET ;
NSString *data = #"";
[seatControl addObject:data];
}
y_offset_row += ROW_COL_OFFSET;
}
UILabel *screenlabel = [[UILabel alloc] init];
screenlabel.frame = CGRectMake((scrollview_w-300)/2, 3, 300, 15);
screenlabel.text = #"Screen";
screenlabel.backgroundColor = [UIColor blackColor];
screenlabel.textColor = [UIColor whiteColor] ;
screenlabel.font = [UIFont fontWithName:#"Helvetica" size:10.0];
screenlabel.textAlignment = UITextAlignmentCenter;
[scrollview addSubview:screenlabel];
scrollview_h = y_offset_row + ROW_COL_OFFSET;
// Adjust scroll view
[scrollview setContentSize:CGSizeMake(scrollview_w, scrollview_h )];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(#"1");
return scrollview;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
NSLog(#"2");
}
In:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(#"1");
return scrollview;
}
you are not supposed to return the UIScrollView itself, rather its subview that you would like to scroll/zoom.
If you have many subviews, I would suggest creating a container view for all of them that you return from viewForZoomingInScrollView; instead of adding your seats to the scrollview itself you would then add them to the container view.
If you want to zoom to a certain seat in the UIScrollView, you should use zoomToRect: animated:. You can specify a certain rectangle (i.e. the seat) to which you would like to zoom in on.
Zooming only works when you implement the viewForZoomingInScrollView: delegate callback.
UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];
self.imageView = tempImage;
scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
scrollView.maximumZoomScale = 1;
scrollView.minimumZoomScale = .37;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
scrollView.zoomScale = .37;
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll {
return imageView;
}
Here my application i having set of button 12 buttons,button titles are hidden by default.Button titles are shown when it is clikced ,
-(IBAction)buttonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int index = button.tag;
[self showing:index]; //now, this method accepts a parameter.
}
-(void)showing:(NSInteger)index
{
UIButton* btn = nil;
index = index - 1;
NSArray* subViewArray = [self.view subviews];
NSInteger i = 0;
for (UIView *view in subViewArray)
{
if ([view isKindOfClass:[UIButton class]])
{
btn = (UIButton*) view;
if(btn.tag >= 1 && btn.tag <= 16)
{
NSString* text;
UIButton *prevButton = (UIButton*) [self.view viewWithTag:previousButtonTag];
if (i == index) {
if ([btn.titleLabel.text isEqualToString:prevButton.titleLabel.text])
{
}
else
{
text=#"";
}
text = [texts objectAtIndex:i]; //put the array that you are using
}
else {
text = #"";
}
i++;
[btn setTitle:text forState:UIControlStateNormal];
}
}// end of IF
} //end of FOR
}
1.I want to compare two button title values to check whether it is equal or not.Im facing the problem ,
2.Only press button title only visible,when i click other button previous title is hided,I want to enable two button title on click and wen it is wrong it should be hided.Please suggest me the How to solve this issue,Please do the needfully
Do some changes with this code , i just try to understand your requirement and add logic for display title of previous button and current button title..
-(void) showing:(NSInteger)index
{
UIButton* btn = nil;
index = index - 1;
NSArray* subViewArray = [self.view subviews];
NSInteger i = 0;
UIButton *btnCurTemp = (UIButton *)[self.view viewWithTag:index];
for (UIView *view in subViewArray)
{
if ([view isKindOfClass:[UIButton class]])
{
btn = (UIButton*) view;
NSString* text;
if (i == index) {
text = [self.textArray objectAtIndex:i]; //put the array that you are using
[btnCurTemp setTitle:text forState:UIControlStateNormal];
UIButton *btnPrevTemp = (UIButton *)[self.view viewWithTag:self.prevButtonTag];
if ([btnCurTemp.titleLabel.text isEqualToString:btnPrevTemp.titleLabel.text]) {
/// visible your both button title..
}
else {
//// hide button title..
}
self.previousButtonTag = i; //make a class variable
}else {
text = #"";
}
i++;
}
}
}
Change your showing like:
-(void) showing:(NSInteger)index
{
UIButton *btn = (UIButton*) [self.view viewWithTag:index];
NSString *text = [self.textArray objectAtIndex:index]; //put the array that you are using
[btn setTitle:text forState:UIControlStateNormal];
if (self.previousButtonTag != -1)
{
UIButton *prevButton = (UIButton*) [self.view viewWithTag:previousButtonTag];
if ([btn.titleLabel.text isEqualToString:prevButton.titleLabel.text])
{
}
else
{
//hide the prevButton
[prevButton setTitle:#"" forState:UIControlStateNormal];
}
}
self.previousButtonTag = index; //make a class variable
}
IButton *btn = (UIButton*) [self.view viewWithTag:index]; will give you the currently clicked button. UIButton *prevButton = (UIButton*) [self.view viewWithTag:previousButtonTag]; give you the previously clicked button. As per your need you can check the titles in the if condition, if they are equal do your stuff there, if not equal then hiding the previous button title.
You need to initialize the previous button tag to -1 in your viewDidLoad
Why don't you simply compare the array strings values using the button tags as the index -
NSString * firstString = [self.textArray objectAtIndex:btn.tag];
NSString * secondString = [self.textArray objectAtIndex:prevButton.tag];
if([firstString isEquleToString:secondString]){
}else{
}
I subclassed UISearchBar in order to set custom image to search results button the following way (.m file):
#import "CustomUISearchBar.h"
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#implementation GipUISearchBar
- (void)layoutSubviews
{
UITextField *searchField = nil;
UIView *backGround = nil;
UIButton *cancelButton = nil;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++)
{
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
{
searchField = [self.subviews objectAtIndex:i];
}
else if ([[self.subviews objectAtIndex:i] isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
backGround = [self.subviews objectAtIndex:i];
}
else if ([[self.subviews objectAtIndex:i] isKindOfClass:[UIButton class]])
{
cancelButton = [self.subviews objectAtIndex:i]; [cancelButton setTintColor:RGB(22.0,38.0,111.0)];
}
}
if(!(searchField == nil))
{
searchField.layer.cornerRadius=15.0f;
searchField.layer.masksToBounds=YES;
searchField.layer.borderColor = [RGB(26.0,141.0,223.0)CGColor];
searchField.layer.borderWidth = 2.0f;
UIImage *glassImage = [UIImage imageNamed: #"search_icon.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:glassImage];
searchField.leftView = iView;
[iView release];
UIImage *historyImage = [UIImage imageNamed:#"history_button.png"];
UIButton *historyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[historyButton setBackgroundImage:historyImage forState:UIControlStateNormal];
[historyButton setBackgroundImage:historyImage forState:UIControlStateSelected];
[historyButton setBackgroundImage:historyImage forState:UIControlStateDisabled];
[historyButton setBackgroundImage:historyImage forState:UIControlStateHighlighted];
historyButton.frame = CGRectMake(0,
0,
historyImage.size.width-3,
historyImage.size.height-3);
historyButton.layer.borderColor = [RGBA(0.0,0.0,0.0,0.0)CGColor];
historyButton.layer.borderWidth = 0.0f;
searchField.rightView = historyButton;
}
if (!(backGround == nil))
[backGround removeFromSuperview];
[super layoutSubviews];
}
The problem is when i touch inside search bar and it resizes i see this button being moved from left to right. How can i disable this behaviour?
try commenting below lines.
[super layoutSubviews];
Because it seems that you are overriding layout of UISearchBar by inheriting it.
So I dynamically create 3 UIButtons (for now), with this loop:
NSMutableArray *sites = [[NSMutableArray alloc] init];
NSString *one = #"Constution Center";
NSString *two = #"Franklin Court";
NSString *three = #"Presidents House";
[sites addObject: one];
[one release];
[sites addObject: two];
[two release];
[sites addObject: three];
[three release];
NSString *element;
int j = 0;
for (element in sites)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//setframe (where on screen)
//separation is 15px past the width (45-30)
button.frame = CGRectMake(a, b + (j*45), c, d);
[button setTitle:element forState:UIControlStateNormal];
button.backgroundColor = [SiteOneController myColor1];
[button addTarget:self action:#selector(showCCView:)
forControlEvents:UIControlEventTouchUpInside];
[button setTag:j];
[self.view addSubview: button];
j++;
}
The #Selector method is here:
- (void) showCCView:(id) sender {
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSString* myNewString = [NSString stringWithFormat:#"%d", whichButton];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor whiteColor];
UINavigationBar *cc = [SiteOneController myNavBar1:#"Constitution Center Content"];
UINavigationBar *fc = [SiteOneController myNavBar1:#"Franklin Court Content"];
UINavigationBar *ph = [SiteOneController myNavBar1:#"Presidents House Content"];
if (whichButton = 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
else if (whichButton = 1)
{
NSLog(myNewString);
[self.view addSubview:fc];
}
else if (whichButton = 2)
{
NSLog(myNewString);
[self.view addSubview:ph];
}
}
Now, it is printing the correct button tag to NSLog, as shown in the method, however EVERY SINGLE BUTTON is displaying a navigation bar with "Franklin Court" as the title, EVERY SINGLE ONE, even though when I click button 0, it says "Button 0 clicked" in the console, but still performs the else if (whichButton = 1) code.
Am I missing something here?
You're using the = operator instead of the == operator in your conditions, which makes an assignment instead of a comparison.
Since the returned value of an assignment is the assigned value, first whichButton becomes 0 and that evaluates to false, and then whichButton becomes 1 and that evaluates to true, and no matter what you do you end up with the Franklin Court option.
You have a problem where you should have used == instead of =.
Instead of this:
if (whichButton = 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
...
Try this:
if (whichButton == 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
else if (whichButton == 1)
{
NSLog(myNewString);
[self.view addSubview:fc];
}
else if (whichButton == 2)
{
NSLog(myNewString);
[self.view addSubview:ph];
}
Or this:
NSLog (myNewString); //occurs in all cases.
switch (whichButton)
{
case 0:
[self.view addSubview:cc];
break;
case 1:
[self.view addSubview:fc];
break;
case 2:
[self.view addSubview:ph];
break;
default:
// optionally handle the case where the button's tag was not 0, 1, or 2.
}