Switching between views in Objective C - ios

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// UIScreen represents the entire screen. Its bounds method
// returns the rectangle that corresponds to the entire screen
// CGRect is a C struct that used to group four floating
// point values that represent the x and y coordinates of the upper left
// corner, as well as the width and the height.
//
CGRect windowRect = [[UIScreen mainScreen] nativeBounds];
UIWindow *window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor lightGrayColor]];
[self setWindow:window];
UIViewController *vc = [[UIViewController alloc]init];
window.rootViewController = vc;
self.mainView = vc.view;
[window makeKeyAndVisible];
//Window is now visible
//Add created views to the mainView to make them visible.
//Example : add a "Hello World" label
UIView *dealerView=[[UIView alloc]initWithFrame:self.mainView.bounds];
UIView *view1=[[UIView alloc]initWithFrame:self.mainView.bounds];
CGRect labelRect = { 150.0, 150.0, 150.0, 50.0 };
UIButton *button1 = [[UIButton alloc] initWithFrame:labelRect];
[button1 setBackgroundColor:[UIColor blackColor]];
[button1 setTitle:#"ENTER HERE" forState: (UIControlState)UIControlStateNormal];
[button1 addTarget:self action:#selector(enterHereClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setUserInteractionEnabled:YES];
[view1 addSubview:button1];
[dealerView addSubview:view1];
return YES;
}
-(void) enterHereClicked:(UIButton *) button{
CGRect labelRect = { 150.0, 400.0, 150.0, 50.0 };
UIButton *sender=button;
UIView *view1=[sender superview];
UIView *view2=[[sender superview]superview];
[view1 removeFromSuperview];
UIView *view=[[UIView alloc]initWithFrame:self.mainView.bounds];
UIButton *button2 = [[UIButton alloc] initWithFrame:labelRect];
[button2 setBackgroundColor:[UIColor blackColor]];
[button2 setTitle:#"Another View" forState: (UIControlState)UIControlStateNormal];
[button2 addTarget:self action:#selector(clickView:) forControlEvents:
UIControlEventTouchUpInside];
[view addSubview:button2];
[view2 addSubview: view];
}
-(void)clickView:(UIButton *) button{
NSLog(#"anand");
UIButton *sender=button;
UIView *view1=[sender superview];
UIView *view3=[[sender superview]superview];
[view1 removeFromSuperview];
CGRect labelRect = { 150.0, 400.0, 150.0, 50.0 };
UIView *view=[[UIView alloc]initWithFrame:labelRect];
[view setBackgroundColor:[UIColor greenColor]];
[view3 addSubview:view];
}
I have edited the code as per you advise. But still not working.
I am creating a new view dealerview and adding and removing the subviews views from it. I am going somewhere wrong but still could not figure it out.
Looks like I am going wrong in the -(BOOL) application method.

It's very atypical to do this sort of thing in the app delegate. It would be better to do this in a subclass of UIViewController and add an instance of that subclass as the rootViewController of the UINavigationController you make key and visible in didFinishLaunchingWithOptions.
You also likely do not want to remove the root view of a UIViewController from it's super view. Currently, you care creating an action for a button that removes its superview, vc.view, from the view hierarchy. The problem is view1 and self.mainView are the same so with:
UIView *view1=[sender superview];
[view1 removeFromSuperview];
[self.mainView addSubview:view];
You're removing a view from the view hierarchy then adding a view to the view that's just been removed and expecting to see it.
You should have one view that acts as a container for button1 and another view that acts as a container for button2 but are both added and removed to/from self.mainView.

UIButton *addVehicleBackbtn=button;
UIView *addVehicleView=[addVehicleBackbtn superview];
[addVehicleView setHidden:YES];
I did hide the View using setHidden:YES in order to switch back between the views.

Related

UIButton in a subview not response to touch events with objective-c

I want to create a bottom tab that could change the view when user press.
To do that I create a view with view controller and sets it's frame in the init
Here is the bottom panel view controller
#implementation BottomTabViewController
-(id) initWithFrame:(CGRect)frame{
if(self = [super init]){
self.view.frame = frame;
return self;
}else{
return nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpButtons];
// Do any additional setup after loading the view.
}
-(void) featureBtnClick:(id*) sender{
NSLog(#"featureBtn Clicked");
}
-(void) favBtnClick:(id*)sender{
NSLog(#"Fav Btn Clicked");
}
-(void) setUpButtons{
UIButton *features = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[features setBackgroundImage:[UIImage imageNamed:#"feature.png"] forState:UIControlStateNormal];
features.backgroundColor = [UIColor greenColor];
[features addTarget:self action:#selector(featureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:features];
UIButton *favBtn = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width - 100, 10, 50, 50)];
[favBtn setBackgroundImage:[UIImage imageNamed:#"favorite.png"] forState:UIControlStateNormal];
[favBtn addTarget:self action:#selector(favBtnClick:) forControlEvents:UIControlEventTouchUpInside];
favBtn.backgroundColor = [UIColor greenColor];
[self.view addSubview:favBtn];
}
#end
And I added that to my view with code like this:
-(void) setUpBottom{
BottomTabViewController *botm = [[BottomTabViewController alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 55, [[UIScreen mainScreen]bounds].size.width, 55)];
botm.view.backgroundColor = [UIColor redColor];
// botm.view.userInteractionEnabled = false;
botm.view.userInteractionEnabled = true;
[self.view addSubview:botm.view];
}
Here here is the result seems, note the bottom panel that works, well pretty silly, but it shows up:
But when press the left and right button, there is not log printed which it expected to be to indict that the button works, What have I done wrong? Thanks
you are adding buttons in the view first then adding the bottom view.
if this is what you are doing then it can be overlapped by the bottom view.
In this case buttons cant be tapped because they have bottom view on top of them.
so make sure u add bottom view first then your buttons. (or change their frames to avoid overlapping)

UIWindow addSubview handle events

I want to build a custom alert by UIViewController and add its view as a subview to the current window. The view displays very well, but I can't handle any touch events.
I have tried many times to add a button to this viewcontroller and add a target. I have also tried adding a UITapGestureRecognizer and set view's userInteractionEnabled to true, but this also failed even when I cleared all subviews just left the button.
Have I missed something?
Here is the code:
CustomAlert Viewcontroller:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
backImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 300) / 2, (self.view.frame.size.height - 250) / 2, 300, 250)];
backImage.image = [UIImage imageNamed:#"AlertBackground"];
[self.view addSubview:backImage];
confirmButton = [[UIButton alloc]initWithFrame:CGRectMake( backImage.frame.origin.x + 100 , backImage.frame.origin.y + 250 - 40, 100, 26)];
[self.view addSubview:confirmButton];
[confirmButton addTarget:self action:#selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
confirmButton.backgroundColor = [UIColor redColor];
[confirmButton setTitle:#"click me" forState:UIControlStateNormal];
}
-(void)confirmButtonClick:(UIButton*)sender{
[self.view removeFromSuperview];
}
-(void)show{
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window addSubview:self.view];
}
Method call custom alert:
CustomAlert * alert = [[CustomAlert alloc]init];
[alert show];
Try to rewrite your -show{} method with these
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window.rootViewController.view addSubview:self.view];
UPDATE:
And if previous don't help, try to overwrite
-(void)show{
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window.rootViewController addChildViewController:self];
[window.rootViewController.view addSubview:self.view];
[self didMoveToParentViewController:window.rootViewController];
}
Those three methods establish parent-child relations.

My UIView has a UIButton as one of it's subviews but not responding to events in objective-c

Here is my code. It has started to look crowded after an hour or 2 of trying to solve this issue. I've tried setting user interaction enabled to yes, I've tried just using a button alone and not making it the subview of another view.
Right now I have filterBar > filterBarContainer > filterButton.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
// Create filter bar with specified dimensions
UIView *filterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[filterBar setUserInteractionEnabled:YES];
// Make it a subview of navigation controller
[[[self navigationController] navigationBar] addSubview:filterBar];
[filterBar setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.9f]];
[filterBar setTag:1];
// Reusable vars
// CGFloat filterBarX = filterBar.frame.origin.x;
//CGFloat filterBarY = filterBar.frame.origin.y;
CGFloat filterBarHeight = filterBar.frame.size.height;
CGFloat filterBarWidth = filterBar.frame.size.width;
// Create centre filter button with specified dimensions
UIView *filterButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 2, filterBarWidth / 2 - 30 , filterBarHeight - 10)];
[filterButtonContainer setUserInteractionEnabled:YES];
// Make it a subview of filter bar
[filterBar addSubview:filterButtonContainer];
[filterButtonContainer setBackgroundColor:[UIColor redColor]];
// Centre the button
[filterButtonContainer setCenter:[filterBar convertPoint:filterBar.center fromView:filterBar.superview]];
// Add button to filter button
UIButton *filterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[filterButton setUserInteractionEnabled: YES];
[filterButton setFrame:CGRectMake(0, 0,40 , 20)];
[filterButton setTitle:#"test" forState:UIControlStateNormal];
[filterButtonContainer addSubview:filterButton];
[filterButton setBackgroundColor:[UIColor yellowColor]];
// self.filterButton = filterButton;
[filterButton addTarget:self action:#selector(filterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
NSLog(#"dd");
filterButtonContainer.layer.borderColor = [UIColor blackColor].CGColor;
filterButtonContainer.layer.borderWidth = 1;
This is the method I'm trying to trigger.
- (void)filterButtonTapped:(UIButton *)sender
{
NSLog(#"filter button tapped");
UIActionSheet *filterOptions = [[UIActionSheet alloc] initWithTitle:#"Filter Garments"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Recommended", #"What's New", #"Price - High to Low", #"Price - Low to High", nil];
[filterOptions showInView:self.collectionView];
}
Most likely one of your parent views (superviews of your button) have a width or height that places the filter button outside of their area. The button is still displayed (if you don't define that the view should cut off subviews), but elements outside the view's area will not get any touch events. Check your superview's width and height.
You're adding filterBar as a subview of your navigationBar, but you're setting it's y-origin within navigationBar to 42. And then you're setting your filterButtonContainer's y-origin within the filterBar to 2. Assuming navigationBar's height is 44, most of the filterBar and the entirety of your filterButtonContainer and filterButton won't be within their navigationBar superview so the button wouldn't be selectable.
Edit: To make it selectable while keeping the positioning intact, I suggest adding the filter bar to self.view. For example, change:
[[[self navigationController] navigationBar] addSubview:filterBar];
to
[self.view addSubview:filterBar];

Removing a programatically created view iOS

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];
}

how can I navigate to another controller without UINavigationControler?

I have following coding in my ViewDidLoad of ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"logo.png"]];
view.frame = CGRectMake(300, 200, 200, 350);
[self.view addSubview:view];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(300, 800, 200, 50)];
label.textColor = [UIColor blackColor];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:30]];
[label setText: #"Tap to Enter"];
[self.view addSubview:label];
[label release]; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Hello, You Tapped !"); }
Now I want to tap and switch to another controller and I want UITabBarController their, how can I do that?
If anyone is not clear, may ask again to me
You can call the new controller as modelviewController..That is one option..and in the viewDidLoad of the newController init tabController there..
UIViewController *newViewController = [[UIViewController alloc] init];
[self presentModalViewController:newViewController animated:YES];
.Or If you want push navigation from right to left..You can animate the view of the new Controller from right to left and push the view of current controller outside to the left of the screen..another option..but I recommend first one...
Take a button on tapping which you should be navigated to another controller. Write a method for the button and in the view did load method of the new controller, initiate the tabbarcontroller

Resources