Capture Event of Subview Class in View Controller - ios

I have two subviews with a UIbutton on them what I want is I want to capture the button click in my View Controller, and change the color of a view residing in it, one way is to create an object of View Controller in subview class and directly change the color of view but I dont want to do that can anybody tell me the other way around.

In your view controller's viewDidLoad, add this:
[subview.button addTarget:self action:(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
where subview.button references the button inside the relevant subview. Also, add this event handler to the view controller:
- (void)buttonWasTapped:(id)sender {
// Button in subview was tapped; change the colour of the view here
}
and implement the logic you need to execute when the button is tapped.

Related

I want to pass touch event for a transparent custom cell to its parent view (which is a pageview controller)?

I'm trying for this solution from many days, I have a scenario where I want to pass touch even (Swiping for page view controller) of my first custom cell to the parent view which is a page view controller.
Here my first cell in the table view is a transparent cell. It is not visible instead of we can see the background view which is a super class of table view controller and it is a page view controller. So here I want to pass the touch event which should be swiping for page view controller. And all the remaining cells are not transparent. Here I'm sending the image so that you can understand. easily. So help me out in passing the swipe/touch to my page view controller Basic Look with transparent cell on top So in that transparent cell area we can see the super view of table view controller which is a page view controller.
As you can see left side is the basic look, and in that you can see the page view controller, and then the right side image is when we scroll the cells it will look like this.
So now you can understand clearly the height of table view and my requirement too.
So how to pass touch/ swipe event to the page view controller when we select transparent cell(indexpath.row ==0 first cell)
I tried the solution that given but no use..
You can add one more view on top of it (pageview -> cellview -> new view on top with opacity = 0). Next add swipe gesture to new view and finally make a newView.delegate = pageviewController to capture swipe event.
#protocol NewViewControllerDelegate<NSObject>
-(void)gestureDidFire;
#end
#interface NewViewController : UIViewController
// you gesture outlet look like IBAction *swipeGestureReconizer;
#property (nonatomic, assign)id <NewViewControllerDelegate>delegate;
#end
//newViewContrlloer.m
- IBAction swipeGestureReconizer
{
if(self.delegate && [self.delegate responseToSelector:#seletor(gestureDidFire)])
{
[self.delegate gestureDidFire];
}
}
//in your page view interface
#interface yourPageView()<NewViewControllerDelegate>
{
//need instance of newView
newViewController.delegate = self;
}
-(void)gestureDidFire
{
//implement what you want
}

Transparent modal view on top of another view

I'm trying to create a modal view using Xcode's storyboard. I want it to consist of a small opaque view within a transparent view, so as to look like what would be called a popup window in desktop terminology.
I've created a segue to the new view controller, set it to "Present Modally" and its presentation to "Current Context", created my opaque view in the new view controller, and then set the background of the view of the new view controller to "Clear Color", and unchecked "Opaque".
When I trigger the segue, the new box slides into place over the previous view, exactly as it should, but then as soon as the animation ends, the background goes black.
What's going wrong here? How can I make the background stay transparent and the old view remain visible?
You can use this category and change the code for you have the transparent modal view. In file UIViewController+ENPopUp.m you just have add [popUpView setBackgroundColor:[UIColor clearColor]]; in the end of presentPopUpView method.
I hope this can help you.
You need to use View Controller Containment:
iOS Developer Library
// Sample code you can use in your UIViewController
func addChildVC(child: UIViewController) {
self.addChildViewController(child)
child.view.bounds = self.view.bounds
self.view.addSubview(child.view)
child.didMoveToParentViewController(self)
}
func removeChildVC(child:UIViewController) {
child.willMoveToParentViewController(nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}

how to get touches of uiimage view in scrollview and perform some action

I have a scrollview with uiimageview1 added on it. On click on that uiimageview1 I want to present a viewcontroller to make some drawing. After drawing, I want to take picture of that drawing and place it on uiimageview1.
i don't know how to and where do i present that viewcontroller for drawing.
please help.
So, first you need to detect touches on your imageView. To do that you need to use UIGestureRecognizer or UIButton.
Then, you need to present a new view controller using the presentViewController:animated:completion: method of your current UIViewController (passing it the 'drawing' view controller).
After the drawing is performed, you can use iOS delegation pattern to notify the original view controller that you just drew something.
The original view controller handles that 'delegated event' and updates the image view accordingly.
UIViewController and UIButton documentations are here for you.
Create a UIButton with uiimageview.layer.bounds and add it as subview to uiimageview
Make the button background color [UIColor clearColor]
Add a #selector to button and push newViewController with segue or add it programaticaly from UINavigation Controller stack.
Otherwise you can make a pan gesture recognizer to UIImageView and listen for user interaction TAP on screen for pushing the new view controller
You can Simply do this by adding UITapGestureRecognizer properties to your image view.
Like:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(presentViewController)];
[self.yourImageView addGestureRecognizer:tapGesture];
-(void)presentViewController{
// do remain stuff
}
Happy Codding :)

Hiding a delete button when you tap outside the view

I have a delete button that shows up when I swipe left on a table view's row.
I want to be able to hide this button if the user taps anywhere else in the view. How do I do that? I tried putting a giant button on bottom of all views but the tap outside is not being detected by the button.
In the viewDidLoad, I added the view controller as a target:
[self.backgroundButton addTarget:self action:#selector(backgroundButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
And in the callback I just have a message:
- (IBAction)backgroundButtonTapped:(id)sender {
NSLog(#"BACKGROUND VIEW TOUCHED");
}
But when I tap outside in a general area, I do not see the message.
I resolved the issue as follows:
. Created a view TopView subclassed from UIView
. Changed class of my top level view to this class.
. Within my logic at the proper place, stored the subview that needs to be hidden in the top view
self.view.mySubview = subview;
. Overrode hitTest:withEvent in top view. Here I detect the tap outside that of the subview and hide the subview as required.

Use destinationViewController button in a custom segue

My goal is to make a custom segue with a custom animation as follows:
I want the segue to cover a button from the sourceViewController with a button from the destinationViewController with an effect similar to the navigation controller's push effect, i.e. the new button is supposed to push the old button away from right to left.
I have been able to make the old button (from the sourceViewController) move away as desired:
[UIView animateWithDuration:1.0 animations:^{
// set the target frame for animated view
sourceViewController.optionsButton.frame = leftButtonTargetFrame;
} completion:^(BOOL finished) {
[navigationController pushViewController:destinationViewController animated:NO];
// reset the button's frame back to its original frame
sourceViewController.optionsButton.frame = leftButtonInitFrame;
}];
But I am struggling to make the new button (from the destinationViewController) move in. The reason is that I cannot access the destinationViewController's view elements: While performing the segue they are not instantiated. And I cannot animate a button that is not instantiated.
So how can I replace a button in the sourceViewController with a button from the destinationViewController?
The view of the destination view controller hasn't been initialized/loaded at the time when you try to access the buttons. To load the view of the destination view controller, you can simply access the view property. Do this before using the buttons: [destinationViewController view];
destinationViewController.view; would also work, but it would generate a compiler warning.
Background Information:
If you access the view property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view.
The method loadView loads the view that the controller manages. You should never call this method directly.
You are correct that you cannot animate an object that does not yet exist. However, you can fake it.
Create a place-holder button that will look identical to the button that will be in the new view controller.
Animate it to the correct place.
As the destination view controller comes in, its button should be invisible.
After the the view controller is in place (i.e. the segue has finished) the destination view controller can ensure the proper placement if its button and make its actual button visible.
Hope this helps.

Resources