Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:07:34+00:00 2026-06-07T07:07:34+00:00

I create a project as Single View Application with initial ViewController; add subViewController to

  • 0

I create a project as Single View Application with initial ViewController; add subViewController to project. UIImageView with pan gesture are added in both of the view controllers.
It works in viewController.m,
but when it is added to subViewController and the subview is added as subview into view controller, the program just crash with “Exc_bad_access”..

Anyone has some solution?

here is my code:

ViewController.m

#import "ViewController.h"
#import "SubViewController.h"

//#define SUB

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

#ifdef SUB
    SubViewController *sb = [[SubViewController alloc] init];
    [self.view addSubview:sb.view];
#else
    UIImageView* img_ = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"monkey_1.png"]];
    img_.userInteractionEnabled = YES;
    UIPanGestureRecognizer *stampPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [stampPanGesture setMinimumNumberOfTouches:1];
    [stampPanGesture setMaximumNumberOfTouches:1];
    [img_ addGestureRecognizer:stampPanGesture];
    img_.center = self.view.center;
    [self.view addSubview:img_];
#endif
}


- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

SubViewController.m

#import "SubViewController.h"
@interface SubViewController ()

@end

@implementation SubViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    UIImageView* img_ = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"monkey_1.png"]];
    img_.userInteractionEnabled = YES;
    UIPanGestureRecognizer *stampPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [stampPanGesture setMinimumNumberOfTouches:1];
    [stampPanGesture setMaximumNumberOfTouches:1];
    [img_ addGestureRecognizer:stampPanGesture];
    img_.center = self.view.center;
    [self.view addSubview:img_];
}


- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:07:38+00:00Added an answer on June 7, 2026 at 7:07 am

    Assuming this is ARC code, the problem is that you’re creating subview’s view controller, grabbing it’s view, but then letting the controller itself fall out of scope and be deallocated.

    The immediate fix would be to make SubViewController *sb an instance variable of the main view controller’s class, that way it won’t go out of scope and won’t be deallocated behind you.

    Thing is, while that will probably fix your crash, the bigger picture problem is that you really shouldn’t be grabbing a view controller’s view and adding it as a subview, but doing nothing with the controller itself. If you do that, for example, various things might not work properly (e.g. rotation events will never reach your subview; anything that iOS needs to communicate to view controllers might not be received by your subview’s controller; I don’t know what in the world didReceiveMemoryWarning situations would do, etc.). In short, Apple advises against this, and encourages you to keep your view hierarchy and your view controller hierarchy synchronized.

    Thus, if you’re transitioning to a new view, you really should just do it via pushViewController or presentViewController. In the unlikely event that this is really a subview (e.g. parts of the prior view will stay on screen and the child view will only take up a portion of the screen), you can use View Controller Containment. You can see WWDC 2011 session 102 for more information on containment. But that’s undoubtedly overkill for what you’re trying to do. Pushing/presenting the subview is probably the logical solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i create new project with the name MyNewProject with a single view application
I've created new Single View project in Xcode. In ViewController.xib I've added new custom
I've made a new project as a Single View iOS Application in Xcode. I've
Using Xcode 4.2.1 iPad iOS 5.0.1, create a new Single View iPad project. In
I created a single view project in xcode 4. I added a navigation bar
I have a project that I created by using Xcode's Single View Application template.
I downloaded xcode 4 and created a single view application with storyboard. I added
I just created a Xcode 4 Single View project (without storyboard), drag and drop
I created an empty Single View application using Xcode 4.2 on OSX Lion 10.7.2
how to create project for native c++ executable in visual studio 2012. In previous

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.