I have created a method for example changeColor for calling the method we use
[self changeColor];
but how can I cancel these method ?
Edited :
here is my code I have several buttons which they add some image to the image view
- (void) setImagesFrame1 {
NSMutableArray *imageArray = [[NSMutableArray alloc]initWithCapacity:12];
for (int i = 0; i <= 12; i++) {
NSString *fileName = [NSString stringWithFormat:#"a%d.png",i];
UIImage *j = [UIImage imageNamed:fileName];
UIImageView *tempImage = [[UIImageView alloc]initWithImage:j];
[imageArray addObject:tempImage];
[self createPageWithImage:tempImage forPage:i];
}
}
- (void) setImagesFrame2 {
NSMutableArray *imageArray = [[NSMutableArray alloc]initWithCapacity:12];
for (int i = 0; i <= 12; i++) {
NSString *fileName = [NSString stringWithFormat:#"s%d.png",i];
UIImage *j = [UIImage imageNamed:fileName];
UIImageView *tempImage = [[UIImageView alloc]initWithImage:j];
[imageArray addObject:tempImage];
[self createPageWithImage:tempImage forPage:i];
}
}
and so on ...
I call my methods with this action :
- (IBAction)openFrames:(UIButton *)sender {
[captureView addSubview:framesPreviewView];
[framesPreviewView sendSubviewToBack:captureView];
framesPreviewView.frame = CGRectMake(img.bounds.origin.x, img.bounds.origin.y, img.bounds.size.width, img.bounds.size.height);
//buttons
if (sender == frame1) { [self setImagesFrame1]; }
if (sender == frame2) { NSLog(#"frame2"); }
if (sender == frame3) { [self setImagesFrame3]; NSLog(#"frame3"); }
}
when I press frame1 button the images will be added to my view , the problem is when I press frame2 button the images of this method also add to my view I need avoid this situation , it means when I touch every button the methods of other button should be canceled
To leave a void function execute the return statement...
return;
The last view on your subview array will be the image, You should use the method [view removeFrom superView];
However, to do that you need to use fast enumeration(for-In loop) over the subviews array.
If you want to be completely sure , you can check the retuned object, if it belongs to UIImageView Class. Then you should execute remaining statements in you setImages method.
Example: `
UIView *tempView;
for(tempView in self.subviews) {
if([tempView isKindOfClass:[UIImageView class] ]){
[tempView removeFromSuperView];
}
}
`
You can:
disable frame2 button when frame1 is pressed
enable frame2 button when setImagesFrame1 ended
Something like this:
- (void) setImagesFrame1 {
frame2.enabled = NO;
[... your code ...]
frame2.enable;
}
So if someone press button 1, he can't press button 2 (and redraw something) until all operations are completed.
Related
Hi I have a method in which I have created 3 views on a click;
-(void)method{
for (i=0; i<3; i++) {
NSLog(#"i is: %d",i);
NSLog(#"i is: %d",i);
userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake (100,41,60,60)];
userResizableView.tag = i;
imageVw1 = [[UIImageView alloc]initWithFrame:userResizableView.bounds];
imageVw1.image = [UIImage imageNamed:#"redacted2.jpg"];
imageVw1.userInteractionEnabled=YES;
imageVw1.autoresizesSubviews = YES;
imageVw1.alpha = 0.93; // for opacity
userResizableView.contentView = imageVw1;
userResizableView.delegate = self;
[userResizableView showEditingHandles];
currentlyEditingView = userResizableView;
lastEditedView = userResizableView;
[self.view addSubview: userResizableView];
[userResizableView release];
}
}
Now in another method I want to hide these views what I created last.
but I am not able to do it. I am hiding my view is like-
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
userResizableView.tag = a;
userResizableView.hidden = YES;
a++;
}
But only one view is hide and the rest are remaining, no matters how amny times I clicked Hide_method, only one view is hide.
My question is that how to hide last view what I created last. Means views hide like 3,2,1,0. On every time when I clicked hide_method.
Any Idea or suggestions would be highly welcome.
Provide unique tag to not clash with any other subview of self.view with same tag like say any number 999 +.
Make changes accordingly:
for (i=0; i<3; i++) {
SPUserResizableView *userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake(100,41,60,60)];
userResizableView.tag = 999+i;
..........
..........
}
Now hide method would be:
-(void)Hide_method{
// find SPUserResizableView with unique tag
for (i=0; i<3; i++) {
if(i == 2){
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+i];
userResizableView.hidden = YES;
}
}
EDIT : To restore frame to original store while allocating view
CGRect prevFrame = userResizableView.frame //take frame while allocating userResizableView
Now restore whenever you wanted to original frame like this:
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+2]; //get last userResizableView
userResizableView.frame = prevFrame;
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
//userResizableView.tag = a;
SPUserResizableView *_view = (SPUserResizableView *)[self.view viewWithTag:a];
userResizableView.hidden = YES;
a++;
}
Try this:
-(void)Hide_method{
//UIView *v = [self.view viewWithTag:a];
SPUserResizableView *v = (SPUserResizableView *)[self.view viewWithTag:a];
if(v)
[v removeFromSuperview];
a++;
}
Method to remove view having tag=a
for (UIView *subview in [self.view subviews])
{
if (subview.tag == a)
{
[subview removeFromSuperview];
}
}
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{
}
-(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;
}
}
}
}
}
I have a navigation based app where I push TableViewControllers onto the stack. I would like to add a background image to the first TableViewControllers. Not a color, but an image.
I have added a categroy for setting and removing the background image.
#implementation UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag {
if(image == NULL){ //might be called with NULL argument
return;
}
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
aTabBarBackground.frame = CGRectMake(self.frame.size.width/2-51,0,102,44);
aTabBarBackground.tag = bgTag;
[self addSubview:aTabBarBackground];
[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
- (void) clearBackgroundImage {
NSArray *subviews = [self subviews];
for (int i = [subviews count] - 1; i >= 0; i--) {
if ([[subviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
}
}
And am setting the image with the following code:
[[navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:#"AppTop-Banner.png"]];
The background image is being added properly but for the subsequent pushed into view Tableviewcontrollers, I can't remove the image by calling the following:
#implementation SecondTableViewController
-(id) init {
self = [super init];
if (self != nil) {
self.title = #"Categories";
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController.navigationBar clearBackgroundImage];
}
In this case "clearBackgroundImage" is never being called and the background image is still there, with the title overlapping the image. I am not sure if I can refer to the navigationBar this way. Maybe self.navigationController.navigationBar is not the right way to call the relevant navigationBar Can somebody throw some light please.
Hai All...
I want to change the uipagecontroller indicator(dot) color...For that i'm doing the below steps.
Create new class named as PageControl with 2 methods
1.- (void) setCurrentPage:(NSInteger)page
2.- (void) setNumberOfPages:(NSInteger)pages
- (void) setCurrentPage:(NSInteger)page
{
NSLog(#"setCurrentPage");
[super setCurrentPage:page];
NSString* imgActive = [[NSBundle mainBundle] pathForResource:#"activeimage" ofType:#"png"];
NSString* imgInactive = [[NSBundle mainBundle] pathForResource:#"inactive" ofType:#"png"];
for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++)
{
UIImageView* subview= [self.subviews objectAtIndex:subviewIndex];
if (subviewIndex == page)
[subview setImage:[UIImage imageWithContentsOfFile:imgActive]];
else
[subview setImage:[UIImage imageWithContentsOfFile:imgInactive]];
subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, 10,10);
}
}
- (void) setNumberOfPages:(NSInteger)pages
{
NSLog(#"setNumberOfPages");
[super setNumberOfPages:pages];
NSString* img = [[NSBundle mainBundle] pathForResource:#"inactive" ofType:#"png"];
for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++)
{
UIImageView* subview= [self.subviews objectAtIndex:subviewIndex];
[subview setImage:[UIImage imageWithContentsOfFile:img]];
subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, 10,10);
}
}
Already i'm having one view with pagecontrol and scrollview...Here what i'm doing is select the uipagecontroller from nib and choose identity inspector and select the class name PageControl(newly created class with 2 methods)it works for changing the color while swipe the images but,it didn't works for clicking the pagecontrol for moving to next page...
I don't know what shoud i want to include or what mistake i'm doing...please help me out to do this...
Thank You...
If you want to enable page scrolling on click of dot then you need to first of all associate the "value changed" property in your xib file to the action you set in your code.
I have implemented the same functionality in one of my projects so it will definitely work.
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
CGFloat pageWidth = scrollView.frame.size.width;
pageWidth = scrollView.frame.size.width;
pageControl.currentPage = page;
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
}
Cheers
Your above code is crashing in iOS 7.I have found a solution for this problem. I know it is not the way but Sure that it works till iOS 8 will be launched in the market.
Reason for Crash:
in iOS 7 [self.subViews objectAtIndex: i] returns UIView Instead of UIImageView and setImage is not the property of UIView and the app crashes. I solve my problem using this following code.
Check Whether the subview is UIView(for iOS7) or UIImageView(for iOS6 or earlier). And If it is UIView I am going to add UIImageView as subview on that view and voila its working and not crash..!!
-(void) updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView * dot = [self imageViewForSubview: [self.subviews objectAtIndex: i]];
if (i == self.currentPage) dot.image = activeImage;
else dot.image = inactiveImage;
}
}
- (UIImageView *) imageViewForSubview: (UIView *) view
{
UIImageView * dot = nil;
if ([view isKindOfClass: [UIView class]])
{
for (UIView* subview in view.subviews)
{
if ([subview isKindOfClass:[UIImageView class]])
{
dot = (UIImageView *)subview;
break;
}
}
if (dot == nil)
{
dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
[view addSubview:dot];
}
}
else
{
dot = (UIImageView *) view;
}
return dot;
}
Hope this solve ur issue too for iOS7. and If Anypone find the optimal solution for that please comment. :)
Happy coding