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

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.

Related

Cause viewDidLoad to run

I'm creating a new UIViewController but waiting a few seconds before I'm showing it.
The thing is I want viewDidLoad to be called before I'm actually showing the now object. How can I make viewDidLoad run?
Currently I'm doing something like this:
UIView *tempView = newObject.view;
But I really don't like that solution.
If there is code that you want to run that you want to be called when the view is instantiated then just put it in the init method.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil
bundle: nibBundleOrNil];
if (self)
{
// Custom stuff
}
return self;
}
Is there another reason why you are wanting viewDidLoad to be called?
Looks like just calling [newObject view]; does the trick!
- (void) viewDidLoad {
[super viewDidLoad];
self.view.alpha = 0; //When alpha = 0, it is also hidden
[UIView animateWithDuration:2.0f animations:^{
// Nothing to animate here
} completion:^(BOOL finished) {
// On animation completion show the content of the view, also animated.
[UIView animateWithDuration:0.5f animations:^{
self.view.alpha = 1;
}];
}];
}
The viewDidLoad is always called before the object is shown in screen.
If you want some delay , you can set some delay code inside viewDidLoad or viewDidAppear. But why exactly do you need the delay , there may be other solutions to solve your issue.

inserting an image in view or delegate

Right now my code was an app delegate, a view controller, and a main view
right now i'm just trying to insert a large image as the background, but i can't figure out where to put it and i'm getting an error when i run a simulation
<Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
- (void) applicationDidBecomeActive: (UIApplication *) application {
NSLog(#"AAAAAAAAAA");
[self startGameLoop];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"AAAAA");
mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
mainView.backgroundColor = [UIColor grayColor];
[self.window addSubview: mainView];
[self.window makeKeyAndVisible];
[self startGameLoop];
return YES;
}
- (void) startGameLoop {
quack = [NSTimer scheduledTimerWithTimeInterval:.0303 target:self selector:#selector(loop) userInfo:nil repeats:YES];
NSLog(#"Game Loop timer instance: %#", quack);
mainView.backgroundColor = [UIColor grayColor];
}
- (void) loop {
NSLog(#"BBBBBB");
pic = [UIImage imageNamed:#"f1.png"];
[pic drawAtPoint:CGPointMake(160, 0)];
[mainView setNeedsDisplay];
}
- (void) drawRect: (CGRect) rect {
maps = [UIImage imageNamed:#"map.png"];
[maps drawAtPoint:CGPointMake(W/2, 0)];
}
You're calling a method ( [pic drawAtPoint:] ) that requires a "current" graphics context, outside of a graphics context. UIKit automatically creates one behind the scenes before any call to drawRect:.
Also, your drawRect must be in the MainView class, not within the UIApplicationDelegate.
Try removing [pic drawAtPoint:CGPointMake(160,0)] from within loop.
If you want to draw it every loop, it must be done within the drawRect method, within the MainView class.
Also, you mentioned you're using a view controller but I see none referenced in your code.
But if you only want to move an image around, you may want to consider NOT calling setNeedsDisplay. Rather, create a UIImageView, add it to your view, and setting its center property within the loop method.

UIView Doesn't Appear and drawRect Method Never Called

I've made a UIView subclass called MiniView.
I try adding it to my viewController as follows:
#interface SomeViewController ()
#property (strong, nonatomic) MiniView *miniView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_miniView];
}
The MiniView class looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"DRAW RECT");
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, self.bounds);
}
But drawRect never gets called, unless I explicity call it, from within the setNeedsLayout method. It also doesn't draw anything. I've tried just adding a UIImageView in the drawRect method, and this appears fine. But the above code produces nothing.
I also get the error:
: CGContextSetFillColorWithColor: invalid context 0x0. This is
a serious error. This application, or a library it uses, is using an
invalid context and is thereby contributing to an overall degradation
of system stability and reliability. This notice is a courtesy: please
fix this problem. It will become a fatal error in an upcoming update.
If I print a log statement in the drawRect method and output the 'rect' values, it's correct at 200 x 200, so I don't know why the context is 0x0.
So one problem is that drawRect is never called, and the other problem is that if I call it explicitly, nothing is displayed...
You probably need to call setNeedsDisplay on your custom UIView subclass:
- (void)viewDidLoad {
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[_miniView setNeedsDisplay]; // Added this
[self.view addSubview:_miniView];
}
This is basically a prod to tell the system your UIView needs to be redrawn. See the docs for more info.
Posting as a separate answer as requested:
The actual problem was that I had redefined the setNeedsDisplay method in the MiniView class, like so:
- (void)setNeedsDisplay
{
NSLog(#"Redrawing info: %#", [_info description]);
}
Because I had neglected to make a call to [super setNeedsDisplay], drawRect was never being called and nothing was drawing. So this fixed it:
- (void)setNeedsDisplay
{
[super setNeedsDisplay];
NSLog(#"Redrawing info: %#", [_info description]);
}
Never call drawRect explicity, try this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
_miniView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_miniView];
[_miniView setNeedsDisplay];
}
Well in your drawrect method include this line:-
[super drawRect:rect];

Can't programmatically move label in viewDidLoad

I'm trying to move a uiLabel down a drop if it's an iPhone 5 (4" display). But it's not working when the code is in viewDidLoad. If I call the code from clicking a uiButton, it works. Here's the code:
-(void) viewDidLoad {
if(CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136))) {
CGRect frame = [self.timeOnCurrentQuestion frame];
frame.origin.y += 40; // change the location
[self.timeOnCurrentQuestion setFrame:frame];
nslog(#"This DOES get logged");
}
}
Jonah, have you tried your code in viewWillAppear method? Possibly, it'll sort-out your issue.
Maybe that are something you need to beware of.
- (void)viewDidLoad
It is a method that when the controller juz created its view.
for example:
maybe in your init method, you call something like:
[self.view setBackground:[UIColor redColor]];
self.timeOnCurrentQuestion = [[UIView alloc] initWithFrame:kFrame];
In this case the work flow will be like this:
[self.view setBackground:[UIColor redColor]];
[self viewDidload];
self.timeOnCurrentQuestion = [[UIView alloc] initWithFrame:kFrame];
Th reason for this work flow is because the self.view is called, then its view is needed before the normal view cycle, so , in this case, self.timeOnCurrentQuestion is still nil in the viewDidload method.
I don't know if my practice is the best or not.
I always init the subView in the controller's init method.
and do the [self.view addSubview:_subview] (//or everything method call that require the self.view) in [self viewDidload];
viewDidAppear worked for me. The life cycle seems to be
LoadView()
viewDidLoad()
viewWillAppear()
viewDidAppear()

Using Instruments to improve memory-management with modal view controllers

I feel like I don't understand something fundamental here. I've been working on memory management in my app while using Instruments to check out live allocations. I have a modal view controller (settingsViewController) that has an image for a background. One thing I noticed was that even after settingsViewController dealloc is called, there still is a live Malloc 520 KB, ImageIO is the responsible library. I'd expect live memory to drop back down after dismissing settingsViewController. Is the UIImageView still hanging around somewhere?
Here is how I load the image in viewDidLoad, as well as dismiss the view controller when I'm finished.
- (void)loadView {
[super loadView];
////// background ////////
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"settings_background.png"]];
[self.view addSubview:background];
[background release];
//////////////////////////
}
- (void)viewDidLoad {
[super viewDidLoad];
///////// done button //////////
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)];
self.navigationItem.leftBarButtonItem = done;
[done release];
////////////////////////////////
}
- (void) doneButtonPressed {
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
NSLog(#"settingsViewController dealloc"];
[super dealloc];
}
At this point, this is all that is in the settingsViewController, so no need to do anything special in dealloc, right? Here is how I'm showing testViewController in the rootViewController.
- (void) loadSettingsView {
SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
UINavigationController *settingsNavigationController = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
[self presentModalViewController:settingsNavigationController animated:YES];
[settingsViewController release];
[settingsNavigationController release];
}
I'd like to make sure I understand what is going on before moving forward. I have several different modal view controllers, each with a different image as a background. Since each one creates a Malloc 520 KB, I end up using 2-3 MB of precious memory for no good reason. What is holding on to that 520 KB?
When you use the +[UIImage imageNamed:] method, the framework caches the image data for you. That's why you see it hold onto some memory even after your view is released. If you're working in the simulator and you want to see it release that memory, send the simulator a memory warning after you've dismissed your view. The image framework should then release the cached image data.