I have created one common view for the application where I have added container view.
This is a confusing structure also very common question for SO but I am stuck since a day..
In my case hierarchy is as
ViewController's View -> (within that) commonview's container view -> (within that)I am adding one another view(base view) with two views.(view 1,view 2)
[self.commonView.containerView addSubview:baseView];
[baseView addSubview:view1];
[baseView addSubview:view2];
[self.view addSubview:self.commonView];
For that I am getting click events for view1's buttons but not for the view2's buttons.
I have checked userInteraction and all other common things. Now I am not getting what is wrong. Also this structure is already used in application so I wont be able to change it.I just have to resolve this issue.
Please check, if you have applied Gesture to any view then make cancelsTouchesInView property of gesture to NO. By default, it is TRUE, so button inside that view may not get touch as gesture cancels its inner view's touch.
If this is not the case, then apply Gesture to your button as this can happen when there are many hierachy of controls.
I think you should try by bringing your subview(i.e. view1) to front, since it is getting hide by another subview (i.e. view2)
You should first try with [self.view bringSubviewToFront:self.commonView.containerView ]; only.
if this works then it's Good otherwise try with this both shown as following
[self.view bringSubviewToFront:self.commonView.containerView ];
[self.view bringSubviewToFront:view1];
Hope this is helpful to you!
Enjoy Programming!
you need to add in respective order.
First add base view1 & view2 into baseView
[baseView addSubview:view1];
[baseView addSubview:view2];
and then add baseView into ContainerView
[self.commonView.containerView addSubview:baseView];
at last add commonView in mainView
[self.view addSubview:self.commonView];
Note :-
If you are adding the viewcontroller into the current viewcontroller then you need to write below code.
[self addChildViewController: yourviewController];
Then your button click will be worked.
Related
I have a ViewController that will be adding two subviews (UIViews) to it depending on what goes on. I want the subview to have UIButtons with events on it but when I do:
[myButton addTarget:self action:#selector(switchCategory:) forControlEvents:UIControlEventTouchDown];
on the subview, nothing responds when the button is pressed.
How do I get this working?
To be more specific, the files would be UIViewController, UIViewA, UIViewB and each UIView file will have its own events. and in the Controller I'm just doing addSubview:UIViewA/UIViewB
Found the fix for it. Being that I didn't give my view a size, it wasn't processing the events.
When dealing with UIView actions/animations, there are 2 main things that you are doing:
1: Make sure you give it a frame, or size to create the visual part.
This can be done using
CGRectMake()
as you probably already know.
2: adding the view on top of the view as a subview.
This can be done by
[self.view addSubview:viewA/viewB];
I have added two swipe gesture recognizers (swipe left and swipe right) to my UITableView. After that my table view has stopped scrolling. At the same time -(void)didSelectRowAtIndex works fine.
What might be the problem?
All I did is drag-and-dropped Swipe Gesture Recognizers from objects library onto my UITableView.
If I remove them, my table starts to scroll again.
UPD:
This happens after upgrading to Xcode 4.5. There is no such problem in older versions of Xcode.
To avoid this behaviour - add UIGestureRecognizers programatically, not in IB.
just try bellow code may this help you....
write bellow code in viewDidLoad: method
UISwipeGestureRecognizer *swipeGestureObjectImg = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(yourSlideOpen_Clicked:)] autorelease];//yourSlideOpen_Clicked is method name where you doing something
swipeGestureObjectImg.numberOfTouchesRequired = 1;
swipeGestureObjectImg.direction = (UISwipeGestureRecognizerDirectionLeft);
[yourView addGestureRecognizer:swipeGestureObjectImg];
UISwipeGestureRecognizer *swipeGestureRightObjectImg = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(yourSlideClose_Clicked:)] autorelease];//yourSlideClose_Clicked is method name where you doing something
swipeGestureRightObjectImg.numberOfTouchesRequired = 1;
swipeGestureRightObjectImg.direction = (UISwipeGestureRecognizerDirectionRight);
[yourView addGestureRecognizer:swipeGestureRightObjectImg];
if here tableview is subview of UIView class then use above code otherwise just try "youtTableview" insted of "yourView"
i hope this help you...
:)
set property of gesture recognizer cancelsTouchesInView = NO
I had the same issue:
I used to drag my TabGestureRecognizer directly onto the storyboard instead of creating them through code. With iOS6 this now seems to break scrolling.
I prefer adding views, gestures, etc directly onto the storyboard instead of through code.
You can still do this but with two changes:
1) Referencing Outlet Collections
Right-click the GestureRecognizer which is visible to you within the scene's dock.
Clear the section Referencing Outlet Collections
2) Attach the GestureRecognizer to the related view through code
Create an outlet for your GestureRecognizer
Add the following line within the viewDidLoad method:
[self.tableView addGestureRecognizer:_tabGesture];
I faced this same problem and I solved it by linking the gesture recognizer to the view instead of to the table view.
Show the storyboard.
Show the connections inspector.
Show the document outline.
Create new link at "Referencing Outlet Collections" between gesture recognizer and view node under the view controller.
Regards.
Pedro.
This is just to confirm my solution:
I have two UIView that I would like to swap between controlled by the UIViewController
In IB I have created 3 UIViews:
TopView
A_View
B_View
Then in ViewDidLoad I add [
[self addSubView] A_View];
Then in some IBAction method I do this
[A_View removeFromSuperview];
[self.view addSubview:B_View];
This is indeed swapping the two views, with TopView just sitting there. But my first thought was just to call [[self view] removeFromSuperView]]; and then add B_view.
This gives me an empty screen?
So is my solution the right way?
Thanks in advance
Regards
Christian
Looks fine to me. It's a pretty normal swapping style, just remove one and add the other.
You should always watch out with modifying the UIViewController's view itself. Your solution is fine.
That way is fine and perfectly straightforward.
If you're on iOS 4.0 or later, you can also use the transitionFromView:toView:duration:options:completion: method in UIView, which removes and adds for you, plus provides a transition animation.
The reason [[self view] removeFromSuperview] left you with a blank window is because you're removing your controller's base view object (including it's subviews, e.g. Top_View, etc) from it's superview (likely the window object). Your solution works when you remove A_View and add B_View, as both are being added or removed as subviews to your controller's view, i.e. self.view.
I'm developing an iPhone app, i have a UIViewController class where some coverflow animations are there and according to design i have some 5 buttons on top of my view each button have an IBAction method on clicking a button i need to display different set of coverflow with different data and the count of coverflow also changes from 1 button action to another. My problem is how can i refresh my page(reload) on click of these buttons, as i know calling explicitly viewDidLoad is a very bad idea, so how can i accomplish this? Any help is appreciated in advance thank you.
The easiest way to get a view to re-draw itself is to call [UIView setNeedsDisplay] or by using [UIView setNeedsDisplayInRect]. This method tells the view that it needs to re-draw itself and it's subviews. However you shouldn't always have to call this. There are other methods that can cause views to redraw themselves. For instance if you add or remove a view to the view hierarchy using [UIView addSubview:] or [UIView removeFromSuperview].
These methods cause the system to call your view's drawInRect: method (assuming you overrode it with custom drawing code). If you did not override this method for custom drawing then it should cause your subviews to receive the same message so that they can get a chance to redraw.
Additional information here. Specifically read the section labeled "The View Drawing Cycle".
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
Why do you want to reload the page?, if it is because you are changing your view and you want it to be redrawn, you can call:
setNeedsDisplay();
But that is often not needed, as whenever a views bounds change that is called automatically for you.
If there's too much other stuff rather than refreshing/reloading the part of your interest in your viewDidLoad method, you can extract the refreshing part into a private method, say methodA. Every time you click your button, you call that methodA. However I think methodA still need call setNeedsDisplay or re-draw the the part of your interest. Hope it helps.
If your point is to refresh "UIViewController", then:
[self viewDidLoad];
Source: How to refresh UIViewController programmatically?
To Refresh your UIViewController, When Clicking the button >>
Add the below code into your UIButton method,
-(IBAction)button:(id)sender{
[self viewDidLoad];
[self viewWillAppear:YES];
}
Now Build and Run Your Code.
Use function viewWillAppear().This function is called everytime the view is called.
I'm creating a simple matching game for kids for iPad. All images are drawn on buttons.
When a matching pair is found,
[matchedBtn1 removeFromSuperview];1
[matchedBtn2 removeFromSuperview];
Now, I'm creating a reset function which allows player to reset the once he/she is done.
Question is how do I re-add the UIButtons</code>?
Tried this,
[self addSubview:matchBtn1];
But the program crashes once reset button is clicked and gives this warning for every button I addSubview to
"viewController" may not respond to "-addSubview".
Many thanks in advance for your help. :)
try :
[self.view addSubview:matchBtn1];
I think you should be trying to add them to the view of the controller, not the view controller itself.
[[viewController view] addSubView:matchBtn1];
Also make sure you have kept a retained copy of matchBtn1 somewhere in your classes as it will have been released when you removed the subviews.
Instead of adding and removing them, create a set of transparent views over the buttons and when you want to remove them, change the color of those views so that the buttons are hidden. That way if you want to reset the screen, you just have to reset all those views to transparent. You may have to set the cover views to intercept touches too, so the user doesn't click hidden buttons.