0

I have a rotating banner at the bottom of my app displaying advertisments, when the user clicks on a rotation a viewcontroller with a simple webview and a toolbar on at the top is display showing a webpage specific to the banner. this all works fine apart from i need a close button in the toolbar. For some reason after i linked the close button to an action that should show 'close' in the log when clicked. i run the app and click the button and a bad access error occurs without a message in the error log.

This is where the viewcontroller with the webview is shown. It is called from a method in the app delegate that can be accessed from anywhere in the app.

BannerViewer* viewer = [BannerViewer alloc];
[viewer setUrl:db.WEBurl];
[_window addSubview: viewer.view];

BannerViewer.h

@interface BannerViewer : UIViewController {
        IBOutlet UIWebView* webView;
        IBOutlet UIBarButtonItem* close;
        IBOutlet UINavigationBar* navBar;
        NSURL *url;
    }

    @property(nonatomic,strong)IBOutlet UIWebView* webView;
    @property(nonatomic,strong)NSURL *url;
    @property(nonatomic,strong)IBOutlet UIBarButtonItem* close;
    @property(nonatomic,strong)IBOutlet UINavigationBar* navBar;
    -(IBAction)closeWindow:(id)sender;
    @end

BannerViewer.m

-(IBAction)closeWindow:(id)sender{
    NSLog(@"close");
}

Thanks for any help in advance!

user987723
  • 945
  • 3
  • 12
  • 33

2 Answers2

1
BannerViewer* viewer = [BannerViewer alloc];

After allocating an object, you need to initialise it by calling a designated initialiser. For subclasses of UIViewController instantiated programmatically, you'll want something like the following:

BannerViewer* viewer = [[BannerViewer alloc] initWithNibName:nil bundle:nil];
Jim
  • 72,985
  • 14
  • 101
  • 108
  • nope, still crashing, its hard to know where to put a break point to get more info when something is linked up through IB – user987723 Aug 30 '12 at 16:17
0

I just encountered this issue as well.

To fix it, I made the new UIViewController into a property.

@property (nonatomic, strong) ThirdViewController *theSecondView;

Based on the tiny amount of information xcode is giving me about this, it looks like it's a memory issue. What seems to be happening is that when the button is pressed the view is created, but ARC releases it's spot in memory soon after it's created, so clicking the button just makes Xcode throw a fit.

If a strong property is used, the new view holds a stronger reference in memory which isn't released until the parent view Controller is also released.

GetSwifty
  • 7,568
  • 1
  • 29
  • 46