Where in ViewController should UIGestureRecognizer be set up? - ios

If ViewController's view is set to MyView
And ViewController maintains a pointer to it's view (set up via storyboards)
#property (weak, nonatomic) IBOutlet MyView *v;
Where in ViewController, should the following take place?
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:v action:#selector(pan:)];
[v addGestureRecognizer: panGesture];

viewDidLoad is a good place for this kind of initialization, since the view hierarchy will have been loaded into memory at that point, whether it was loaded from a storyboard or nib, or created programmatically.

Since it appears you are using a XIB file for your view, viewDidLoad is proper.

Related

Child view controller's view is not shown in a container

I have a scene in the storyboard with a Container View. As you may suppose, I want its associated UIViewController to load there another UIViewController as a child view controller, and to show its view as a subview.
So, in the parent UIViewController I defined the properties:
#property (weak, nonatomic) IBOutlet UIView *containerView;
#property (strong, nonatomic) MyChildViewController *childController;
Then, in the viewDidLoad method of the parent, I do:
if (self.childController == nil) {
self.childController = [[MyChildViewController alloc] init];
}
[self addChildViewController:self.childController];
self.childController.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
[self.containerView addSubview:self.childController.view];
[self.childController didMoveToParentViewController:self];
Both self.childController.view and self.containerView are not nil, but self.childController.view is not displayed when I run the app... What can I be missing?
Thanks
I finally noticed what I was overlooking... since I also created a scene in the storyboard for MyChildViewController, I needed to instantiate it from there.

Memory not released after removing subviews (ARC)

I am trying to solve "Received Memory Warning" issue.
My app has 2 view controllers and when you click a button on first view controller,
detail view controller appears.
The detail view controller has a view inherited of UIView called 'topView' and the view has many subviews.
The subviews are also inherited of UIView and each subview has 2 UILabels.
My question is when you go back to first view controller by clicking back button,
'topView' is not released, even if I put the following code in viewDidDisappear.
How can I release the memory of topView?
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[self.topView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
self.topView= nil;
}
Am I missing some thing?
I think I'm heading to some wrong direction, so please give me advice.
I'm not sure, but here's a thought. When you call this line:
[[self.topView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
It removes all of the subviews which declare self.topView as a superview. The next line:
self.topView = nil
Doesn't remove the top view itself, but rather nil's your pointer to it. Because views are retained by their superviews, this object will stick in memory until the view controller is removed and its view is released.
Check your variable declarations (weak, strong) to be sure that the topView object hasn't retain somewhere else.
Such as IBOutlet's usually declared with a Weak Key:
#property (weak, nonatomic) IBOutlet UIView * topView;

Display UIview controller as a subview using storyboard IOS7

in my application I have a UIView object in second view controller. Now I want to display the UIView as a popup or sub view when I click the button from first view controller. Please tell me how to do this? I have seen many solution for the nib files but I didn't find any for storyboard.
I have connected the IBOulet of view in my second view controller.
#property (strong, nonatomic) IBOutlet UIView *popview;
And i have imported the second view controller in my firstview please tell me how to make this one i have been stuck here for long time please help me out on this one.
Thanks.
In your FirstViewController which has UIView *popview:
- (void)viewDidLoad
{
[super viewDidLoad];
// you don't want to show PopView
self.popview.alpha = 0;
}
- (void) showPopView
{
//you want to show PopView
self.popview.alpha = 1;
}
Try Adding this in your code
-(void)viewDidLoad
{
popView.hidden=true;
}
and when ever user clicks an action change the hidden property to false just like
-(IBAction)AnyActionPerferformed:(id)sender
{
popView.hidden=false;
}
You can Learn about this from
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html
You don't need an extra UIViewController
In your storyboard, drag a new UIView to your view controller. (It will be your popup view)
Link to your view controller. (#property (strong, nonatomic) IBOutlet UIView *popview;)
When you want to hide popview set it's alpha to 0.

Create Container view for Two UIViewController on iPad

i have two UIViewController class with xib from interface builder, and i want create another UIViewController with xib that contains this two view, it's possible and how i can achieve this feature?
You can't add View controller A and B's views to view controller C in an XIB because there is a fair amount of housekeeping code you need to do to set up the parent/child view controller relationship.
However, if you were to use storyboards, and you are running in iOS 6 or later, this would be trivial. There is a new "container" view in iOS 6. You just add a container view to your scene, and then control-drag from the container view onto another view controller's scene, and IB sets up an embed segue that does all the housekeeping for you. In my view this feature alone is enough to justify moving to storyboards.
You add two UIView in your third UIViewController xib and you link these UIView with IBOulet.
#property (nonatomic, weak) IBOutlet UIView *firstView;
#property (nonatomic, weak) IBOutlet UIView *secondView;
After that you add your UIViewController in theses subviews in the viewDidLoad for example :
- (void)viewDidLoad
{
[super viewDidLoad];
MyFirstViewController *myFirstViewController = [[MyFirstViewController alloc] initWitNibName:#"MyFirstViewController"];
MySecondViewController *mySecondViewController = [[MySecondViewController alloc] initWitNibName:#"MySecondViewController"];
[self.firstView addSubview:myFirstViewController.view];
[self.secondView addSubview:mySecondViewController.view];
}
Improve it with #property of First and Second ViewController and Lazy loading.

How to get custom UIView from Storyboard

In FatEditViewController, has a custom UIView.
FatEditViewController.h
#import <UIKit/UIKit.h>
#interface FatEditViewController
#property (weak, nonatomic) IBOutlet UIView *myview;
#end
In another UIViewController, I want to get the myview object.
I do like this:
FatEditViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"FatEdit"];
UIView *view = [vc myview];
NSLog(#"%#, %#", vc,view);
And the myview is null.
How to get the myview?
The problem here is that your myview gets instantiated only when your FatEditViewController's method viewDidLoad is called, which of course is called only when the view controller is presented on screen. One possible way to achieve what you need is to build myView programmatically (it wouldn't be an IBOutlet anymore), possibly by lazy initialising it in the getter. You would have to add the view to your FatEditViewController in the viewDidLoad method programmatically as well.
-(UIView *)myview
{
if(!_myview){
_myview = [[UIView alloc] initWithFrame:whateverFrameYouNeed];
//build the view
}
return _myview;
}
The property would have to be strong as well.

Resources