Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewControllerWrapperView buttonPressed]: - uibutton

I've been trying to search for solution, but answers to similar problems didn't quite point me in right direction. The problem is as follows
in my iOS app i create a custom image view atop of my navigation controller. this is done via alloc initing BannerView Class from designated view controllers
e.g. in viewDidLoad of my MasterViewController i call
BannerView *bannerLogoView = [[BannerView alloc]init];
//add the tabcotroller as a subview of the view
[self.tabBarController.view addSubview:bannerLogoView.bannerView];
BannerView.m
#import "BannerView.h"
#interface BannerView ()
#end
#implementation BannerView
-(id)init {
self = [super init];
if (self) {
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bannerImage.jpg"]];
self.bannerView = [[UIView alloc] initWithFrame:CGRectMake(0,20, 320, 30)];
[self.bannerView addSubview:self.imageView];
//NSLog(#"Setting up image from within banner view");
[self setupButton];
}
return self;
}
- (void)buttonPressed {
NSLog(#"Button pressed");
}
-(void) setupButton {
self.imageView.userInteractionEnabled = YES;
self.button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
self.button.frame = CGRectMake(0.0, 0.0, 320.0, 30.0);
[self.imageView addSubview:self.button];
}
Now anytime the button is clicked i get either EXC_BAD_ACCESS with no console output, or
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewControllerWrapperView buttonPressed]: unrecognized selector sent to instance
Could anyone help me out? I might be doing something wrong with topology/inheritance here, but I can't find a simple way to find this out..
Thank you in advance

Not sure what's causing your problem but you do have some weird constructs here.
First, usually buttons are not added to UIImageViews. Instead, add them both to a common view and then call the common view's -bringSubviewToFront: to overlay the button onto the image view.
Second, the selector takes the sender as an argument, so your selection should be buttonPressed: and method should be -(void)buttonPressed:(id)sender;

Related

Unbalanced calls to begin/end appearance transitions for <xxViewController: 0xaa82610>

I tried to add TapforTap Ads to my iPhone app, the ad appear but I show this message in console "Unbalanced calls to begin/end appearance transitions for ." after any navigation of pages and then the app crash, the below code for calling TapForTap Ads. How I can solve this problem?
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat y = self.view.frame.size.height - 50.0;
TapForTapAdView *adView = [[TapForTapAdView alloc] initWithFrame: CGRectMake(0, y, 320, 50) delegate: self];
[self.view addSubview: adView];
[TapForTapAppWall prepare];
[TapForTapAppWall showWithRootViewController: self]; // or possibly self.navigationController
}
Thanks A lot
I don't know it for sure. But try this:
- (void) ShowTapAd {
CGFloat y = self.view.frame.size.height - 50.0;
TapForTapAdView *adView = [[TapForTapAdView alloc] initWithFrame: CGRectMake(0, y, 320, 50) delegate: self];
[self.view addSubview: adView];
[TapForTapAppWall prepare];
[TapForTapAppWall showWithRootViewController: self]; // or possibly self.navigationController
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorOnMainThread:#selector(showTapAd) withObject:nil waitUntilDone:NO];
}
The run time warning that you got appears when you hussle with two (or more) segue, Navigation Controller push or present modally types of processes. Meaning when you initiate one before the former was totally finished.
I am surprised that those things can happen when you call something within viewDidLoad. So you may find out that the root cause is not located within the code sniplet that you have shown. But if it is then this could do the trick.
What it does:
It just makes sure that your setup of the ad view is performed after viewDidLoad is properly finished. As viewDidLoad runs on the main tread as all UI related stuff does (or should do), the current appearance transistion should be finised by then.
Hope, this helps. Again, it is just a guess.

Reading touch events in a QLPreviewController

I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?
I tried the following, but no events seem to trigger when I test it.
/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>
#interface TouchPreviewController : QLPreviewController
#end
//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"
#implementation TouchPreviewController
- (id)init:(CGRect)aRect {
if (self = [super init]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerDTap];
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
//[singleFingerDTap release];
}
return self;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
NSLog(#"TouchPreviewController tap!" ) ;
}
// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
return YES;
}
#end
Edit: For clarification, this is how I instantiate the controller:
documents = [[NSArray alloc] initWithObjects: filename , nil ] ;
preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width;
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
//add it
[quickLookView addSubview:preview.view];
Also, I've defined the callback methods as this:
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}
Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?
Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since
gesture recognizer is not active.
I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)
I have never used the class so I could be way out in left field.
If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.
I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.
I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.
When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.
Hope this is of some help.

Correct method to programmatically create UIView and UIViewController and link together

I'm new to iOS development. To keep my iOS app nicely compartmentalised I'd like to create both the UIView and the UIViewController programatically, and tie them together once created.
So, I do the following: in my view controller I have this:
-(void)loadView {
NSLog(#"HPSMainMenuViewController loadView starting");
HPSMainMenuView* mainmenuView = [[HPSMainMenuView alloc]initWithFrame:CGRectZero];
self.view = mainmenuView;
}
and in my View I have this:
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"HPSMainMenuView initWithFrame starting");
[self setup];
}
return self;
}
-(void)setup {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = E_PROFILE_BUTTON;
[btn setTitle:#"Option1" forState:UIControlStateNormal];
[self.view addSubview:btn ];
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = E_CONTACTS_BUTTON;
[btn setTitle:#"Option2" forState:UIControlStateNormal];
[self.view addSubview:btn ];
self.title = #"Hello";
}
Is this the right way to do this (given I want full programmatic control). It seems wrong to dynamically build the view within the ViewController hence my approach where I am building the view within an actual UIView class.
Lastly, I'm using loadView; should I be using viewDidLoad? If so, why?
Thanks very much.
What you are doing is correct, and actually good. Many developers keep their view controllers and views heavily tied together but if you want to keep them separate then that's great.
loadView is where you should be creating and initializing everything. viewDidLoad can be called multiple times if the view is unloaded/reloaded due to memory warnings (for example). So viewDidLoad is where you would restore saved state, make your view correctly reflect your model, or any other initialization that you can't do in loadView.

Game Center Calling Leaderboard function not responding?

I am calling the leader board like this:
-(void)viewscores:(SPEvent*)event
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview: self.rootViewController];
[self presentModalViewController: leaderboardController animated: YES];
}
}
When I Click the leader board button, I receive an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIViewController presentModalViewController:animated:]: unrecognized selector sent to class 0x3e2fc7f8'
Is this normal?
You should probably call the function in a viewController. In one of my app's the code looks like this:
-(IBAction)showLeaderBoard {
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
if (leaderBoardCont) {
leaderBoardCont.category=#"1S";
leaderBoardCont.timeScope=GKLeaderboardTimeScopeWeek;
leaderBoardCont.leaderboardDelegate=self;
[self presentModalViewController:leaderBoardCont animated:YES];
}
}
and then you should also implement the delegate method:
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
viewController = nil;
}
Here 1S is the identifier for the leader board you created in iTunes Connect. Hope this helps.
Edit: since you are still having problems, check these tutorials out. They cover everything about leader boards and achievements.
1st part
2nd part
You're sending the presentModalViewController message to an object that doesn't recognize it. So the class you wrote in your "Game.m" file doesn't inherit from UIViewController. Dunno what framework you're using, but you'll have to init a UIViewController instance to show the GameCenter view.

Create subview on awakeFromNib

I'm trying to create a NSImageView programmatically as a subview of another NSImageView when awakeFromNib is called.
My code is as follows (Fader is defined in MyImageView.h):
#implementation MyImageView
- (void)awakeFromNib {
Fader = [NSImageView initWithFrame: [self frame]];
}
I get the warning message "NSImageView may not respong to +initWithFrame". When I build, the app simply frizzes without showing anything, and I have to "force quit".
What am I doing wrong?
You’ve forgotten to send +alloc in order to allocate the object. Change that line to:
Fader = [[NSImageView alloc] initWithFrame: [self frame]];