I'm adding a subview to the main view but then I cant remove it from this view.
NextViewController *nextView = [[NextViewController alloc] init];
nextView.transitioningDelegate = self;
nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.presentedViewController.view addSubview:nextView.view];
nextView.view.frame = CGRectMake(724, 80, 300, 150);
UIButton *stayButton = [[UIButton alloc] initWithFrame:CGRectMake(101, 94, 90, 49)];
[stayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[stayButton setTitle:#"Detener" forState:UIControlStateNormal];
[stayButton addTarget:self
action:#selector(waitForNextView)
forControlEvents:UIControlEventTouchUpInside];
[nextView.view addSubview:stayButton];
stayButton.titleLabel.font = [UIFont systemFontOfSize:25];
[stayButton setTitleColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1] forState:UIControlStateNormal];
here I'm adding a button to the view and then when the button is pushed it goes`-
(void)waitForNextView{
transition = NO;
NextViewController *nextView = [[NextViewController alloc] init];
SectionViewController *sectionView = [[SectionViewController alloc]init];
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
actualSection = 0;
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(dismissView2) userInfo:nil repeats:NO];
}
I've tried with:
[nextView.view removeFromSuperView];
[nextView.presentedViewController removeFromParentViewController];
But I don't know what else can I do :S
If you want more information, nextView is a UIViewController of 300x100 size. I want to present it and then remove it when I hit the button, that's all.
I would be great if someone can help me.
Thanks
You are creating different reference and then you're trying to remove it from its superview while not being added at all. I will try to explain:
1. When you're adding nextView.view to self.view you have created a instance like this
NextViewController *nextView = [[NextViewController alloc] init];
2. When you're trying to remove it, instead of referring to the instance above you're creating a brand new one again in your waitForNextView method like so again
NextViewController *nextView = [[NextViewController alloc] init];
and then you're trying to remove it. Please note that the last instance of nextView has not being added anywhere yet, and this is the reason your code doesn't work.
In order to work you should refer to the first reference you've added to self.view. Please adjust your code as shown below
In your ViewController.m
#interface ViewController()
{
NextViewController *nextView;
}
When adding your viewController
nextView = [[NextViewController alloc] init];
nextView.transitioningDelegate = self;
nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.presentedViewController.view addSubview:nextView.view];
nextView.view.frame = CGRectMake(724, 80, 300, 150);
UIButton *stayButton = [[UIButton alloc] initWithFrame:CGRectMake(101, 94, 90, 49)];
[stayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[stayButton setTitle:#"Detener" forState:UIControlStateNormal];
[stayButton addTarget:self
action:#selector(waitForNextView)
forControlEvents:UIControlEventTouchUpInside];
[nextView.view addSubview:stayButton];
stayButton.titleLabel.font = [UIFont systemFontOfSize:25];
[stayButton setTitleColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1] forState:UIControlStateNormal];
And finally in your IBAction method
-(void)waitForNextView{
transition = NO;
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
actualSection = 0;
[nextView removeFromSuperview];
}
The problem here is that you are adding the button as a subview to the view of nextView, so removing the view of nextView won't work.
If you add a tag to your button, then you can retrieve the subview by using the tag. Eg. When you define your button:
[stayButton setTag:1];
Then in the waitForNextView method, you can get the control like this:
UIButton *stayButton = (UIButton*)[nextView.view viewWithTag:1];
[stayButton removeFromSuperView];
use this code in the methode dismissView2
for (UIView *subview in view.subviews ){
[subview removeFromSuperview];
}
Related
In my app I have a UINavigationController with a UISplitViewController as rootViewController. The masterViewController of this UISplitViewController sets a UIView with a UITextField centered inside it as titleView. This is wat it looks like in the storyboard.
- (void)viewDidLoad {
[super viewDidLoad];
self.titleView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-((self.view.frame.size.width/2)+150))/2, 0, (self.view.frame.size.width/2)+150, 30)];
self.rightButton = [[UIButton alloc] init];
[self.rightButton setImage:newImage forState:UIControlStateNormal];
[self.rightButton addTarget:self action:#selector(barButtonRight:) forControlEvents:UIControlEventTouchUpInside];
self.rightButton.frame = CGRectMake(0, 0, 30, 30);
self.rightButton.tintColor = [UIColor whiteColor];
[self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
self.splitViewController.navigationItem.rightBarButtonItem = rightButton;
self.textField = [[UITextField alloc]init];
if (IDIOM == IPAD) {
self.textField.frame = CGRectMake(0, 0, self.titleView.frame.size.width, 30);
} else {
self.textField.frame = CGRectMake(65, 0, (self.view.frame.size.width-20)-110, 30);
}
self.textField.backgroundColor = [UIColor purpleColor];
if (IDIOM == IPAD) {
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
self.textField.leftView = paddingView;
self.textField.leftViewMode = UITextFieldViewModeAlways;
}
[self.titleView addSubview:self.textField];
self.splitViewController.navigationItem.titleView = self.titleView;
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
Now, when a the right button is pressed a strange thing happens.
- (IBAction)barButtonRight:(id)sender {
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
The UITextField is pushed all the way to the left inside the UIView. I commented out most of my viewDidLoad code and this is still happening. Any ideas as to what's causing this?
I have a view controller which is being added as a child view controller. The problem is that after the child controller creates its view, I am not able to modify it.
It loads everything properly, but when I want to hide a text field on clicking a button (textfield.hidden = YES), nothing happens!
This is my loadView :
-(void)loadView {
self.waitingPopup = [[WaitingPopupViewController alloc]initWithLabel:#"loading salon"];
self.view=[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
// self.view.frame=CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height);
scrollableTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollableTable.delegate=self;
scrollableTable.dataSource=self;
[self.view addSubview:scrollableTable];
headerHolder=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
//creating and adding backgroundImage view container and setting the image
// saloonHomeImage.backgroundColor = [UIColor grayColor];
saloonHomeImage.frame=CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
saloonHomeImage.contentMode = UIViewContentModeScaleAspectFill;
//[saloonHomeImage setBackgroundColor:[UIColor redColor]];
[headerHolder setBackgroundColor:[UIColor grayColor]];
[headerHolder addSubview:saloonHomeImage];
//creating and adding saloonheader label
saloonLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 32, self.view.frame.size.width, 48)];
saloonLabel.text=#"";
saloonLabel.textAlignment=NSTextAlignmentCenter;
[saloonLabel setTextColor:[UIColor whiteColor]];
[saloonLabel setFont:[UIFont fontWithName:#"vevey" size:48]];
[headerHolder addSubview:saloonLabel];
//creating and adding book appointment button
UIButton *bookAppointmentButton=[UIButton buttonWithType:UIButtonTypeSystem];
NSLog(#"%f", self.view.frame.size.height-97.5);
bookAppointmentButton.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.size.height-97.5, 216, 53);
[bookAppointmentButton setBackgroundImage:[UIImage imageNamed:#"bg-left.png"] forState:UIControlStateNormal];
[bookAppointmentButton setTitle:#"BOOK APPOINTMENT" forState:UIControlStateNormal];
[bookAppointmentButton addTarget:self action:#selector(bookAppointmentAction) forControlEvents:UIControlEventTouchUpInside];
[bookAppointmentButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[headerHolder addSubview:bookAppointmentButton];
UIButton *callButton=[UIButton buttonWithType:UIButtonTypeSystem];
callButton.frame=CGRectMake(216, self.view.frame.size.height-97.5, self.view.frame.size.width-216, 53);
[callButton setBackgroundImage:[UIImage imageNamed:#"bg-right.png"] forState:UIControlStateNormal];
[callButton addTarget:self action:#selector(callButtonAction) forControlEvents:UIControlEventTouchUpInside];
[callButton setTitle:#"CALL" forState:UIControlStateNormal];
[callButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[headerHolder addSubview:callButton];
[self getSalonDetails];
loading=[[UIImageView alloc] init];
loading.frame=CGRectMake(130,200,40,36);
[loading setBackgroundColor:[UIColor clearColor]];
loading.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"spinner-1.png"],
[UIImage imageNamed:#"spinner-2.png"],
[UIImage imageNamed:#"spinner-3.png"],
[UIImage imageNamed:#"spinner-4.png"],
[UIImage imageNamed:#"spinner-5.png"],
[UIImage imageNamed:#"spinner-6.png"],
[UIImage imageNamed:#"spinner-7.png"],
[UIImage imageNamed:#"spinner-8.png"],[UIImage imageNamed:#"spinner-9.png"],nil];
loading.animationDuration = 1.0f;
loading.animationRepeatCount = 0;
[loading startAnimating];
[self.view addSubview:loading];
loadingText = [[UILabel alloc]initWithFrame:CGRectMake(90, 240, 200, 40)];
loadingText.text = #"loading salon";
[loadingText setFont:GOTHIC_FONT(22)];
[loadingText setTextColor:[UIColor whiteColor]];
[self.view addSubview:loadingText];
articles=[Model getArticles];
}
- (void) getSalonDetails{
[[MyDataModel sharedDataModel] getSalonDetails];
}
This is the delegate method which is trying to set some fields hidden. This delegate is called when an NSURLSession call completes successfully. The control comes here as per logs but nothing gets hidden. When I add a breakpoint and inspect the fields, every object is shown as nil. If this class is used as the main view controller, I've found everything works. On making it a child view, nothing works.
-(void)salonDetailsRecievedDelegate:(BOOL)success andStatusCode:(int)statusCode{
[loading stopAnimating];
self.loading.hidden = YES;
self.loadingText.hidden = YES;
saloonLabel.text = #"this is texT";
if(success){
//
}
}
What I think here is that salonDetailsRecievedDelegate isn't called from the main thread, try any of the following:
1) call it in the main thread from your model as follows:
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate salonDetailsRecievedDelegate:success andStatusCode:status];
});
2) change its implementation as follows:
-(void)salonDetailsRecievedDelegate:(BOOL)success andStatusCode:(int)statusCode{
dispatch_async(dispatch_get_main_queue(), ^{
[loading stopAnimating];
self.loading.hidden = YES;
self.loadingText.hidden = YES;
saloonLabel.text = #"this is texT";
if(success){
//
}
});
}
I have created a UIButton and UIImageView where the button frame is set same as image frame size. What I'm willing to do is once I touch the image it should PopOver. Button is set with action.The problem is that it is displaying only image once I touch it is not Poping up.
What is wrong with my code?
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)];
imageView.image=[UIImage imageNamed:#"abc.jpg"];
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
UIButton *imageButton = [[UIButton alloc]init];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
[imageButton release];
[imageView release];
}
-(void)imageTapped:(UIButton*)in_sender {
UIViewController *popoverContent = [[UIViewController alloc] init];
CGRect theScreenFrame = [[UIScreen mainScreen] bounds];
popoverContent.view.frame = CGRectMake(theScreenFrame.origin.x, theScreenFrame.origin.y,100, 100);
popoverContent.view.backgroundColor = [UIColor whiteColor];
popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300);
[popoverContent.view addSubview:imageButton];
if(m_DetailPopover) {
[m_DetailPopover release];
m_DetailPopover = nil;
}
UIPopoverController m_DetailPopover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverContent release];
[m_DetailPopover presentPopoverFromRect:imageButton.frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Might be the problem is a typo in the code:
UIPopoverController *m_DetailPopover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
Also pls note that you cannot reference imageButton in imageTapped, because
you release it in viewDidLoad.
Dont' add an image view as subview to a button or vice versa. Just assign the image (not the image view) as background image to the button.
Your UIImageView is front of the UIButton, not allowing to get the button action. Why don't you create a custom button with your image?
Do like this:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setImage:[UIImage imageNamed:#"abc.jpg"]];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
[imageButton release];
[imageView release];
EDIT
UIPopoverController *m_DetailPopover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[m_DetailPopover presentPopoverFromRect:imageButton.frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverContent release];
in my game app i created a view programatically with a uibutton.
by clicking the button the whole view shall be removed. but i had no success with all my attempts to remove the view with "[polygonView removeFromSuperview];" for example.
I think i need to
get the current view first but have no idea how to do that and to implement that in the
button method.
here is the code:
- (void) addNewView {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 60, 0, 900, 900)];
polygonView.backgroundColor = [UIColor blueColor];
polygonView.tag = 42;
// [self playMovie];
[window addSubview:polygonView];
// [polygonView setHidden:NO];
[polygonView release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[polygonView addSubview:button];
// NSLog(#"mike=%# tag=%d",[[polygonView class] description], [polygonView tag]);
}
-(void)buttonPressed {
NSLog(#"Button Pressed!");
[polygonView removeFromSuperview];
}
You need to get a hold of your polygonView:
-(void)buttonPressed {
UIView *polygonView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
[polygonView removeFromSubview];
}
After looking through the post on this simple topic I still can not figure out what I've done wrong here. I am trying to make the buttons loop through the same action in the same viewController.
When the view comes onto the screen the buttons do not click (or at least the NSLog does not write to the console which it does the first time it enters this code.)
- (IBAction)answer: (id) sender{
NSLog(#"The Number is: %d\n",qNumber);
qNumber+=1;
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[newView setUserInteractionEnabled:TRUE];
UIImage *buttonImage = [UIImage imageNamed:#"pink_button.png"];
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
[buttonImageView setImage:buttonImage];
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
buttonLabel.text = #"newLow";
buttonLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 15.0];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textAlignment = UITextAlignmentCenter;
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton addSubview:buttonImageView];
[lowButton addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
}
Thanks for any help you can provide...even if I missed something simple :-)
Latest Updated Code:
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newView addSubview:buttonImageView];
[newView addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
[self.view bringSubviewToFront:lowButton];
self.view = newView;
Still when the button is pressed it does not enter the action code.
You button needs a frame. You can't tap a frameless button.
lowButton.frame = CGRectMake(23, 294, 72, 37);
Also, when you add label and image to that button, their frame should have x and y 0.
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
But may I ask why you are adding new UIImageView and UILabel to UIButton when it has it's label and background view properties?
Edit here is a cleaned-up code for you:
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton setImage:[UIImage imageNamed:#"pink_button.png"] forState:UIControlStateNormal];
[lowButton setTitle:#"newLow" forState:UIControlStateNormal];
[lowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[lowButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
Notice I also removed [newView setUserInteractionEnabled:TRUE]; as it's not needed since it's enabled by default.
Your custom button's subviews are covering the button, so it does not get any clicks.
Try adding all the subviews to newView rather than to lowButton (including lowButton itself) and then call
[self.view bringSubViewToFront:lowButton];