UIWebView appears null when calling fro a method - iphone

I have a major problem when trying to access a UIWebView that was created during ViewDidLoad, the UIWebView appears null
here is how i declare the property
#property (nonatomic, retain) UIWebView *detailsView;
the implementation
#implementation iPadMainViewController
#synthesize detailsView;
- (void)viewDidLoad
{
[super viewDidLoad];
detailsView = [[UIWebView alloc] initWithFrame:CGRectMake(500, 0, 512, 768)];
[self.view addSubView:detailsView];
}
When accessing from
- (void)loadDetailedContent:(NSString *)s
{
NSLog(#"%#", detailsView);
}
I get NULL, is it a normal behavior or am i doing something wrong?
here is the touchesBegan that is being called, from the views subclass that is being touched,
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
iPadMainViewController *mycontroller = [[iPadMainViewController alloc] init];
self.delegate = mycontroller;
[self.delegate loadDetailedContent:NewsId];
}

1st of all change you synthesize to
#synthesize detailsView = _detailsView
and allocate your uiwebvie
_detailsView = [[UIWebView alloc] initWithFrame:CGRectMake(500, 0, 512, 768)];
And try to push your ipadMainController in touchBegan to main screen and in vieDidLoad call loadDetailedContent

Are you sure that loadDetailedContent is not called before viewDidLoad? Set breakpoints in both methods and see which one gets hit first.
EDIT Since you updated the question with the code, it is easy to see why this problem happens. In touchesBegan, you instantiate the UIViewController and then call loadDetailedContent. This means that loadDetailedContent will be called before viewDidLoad.
viewDidLoad is called first when the view it controls has been created.

Related

RootViewController - iPhone

I am new to iPhone programming and trying to grasp the concept of RootViewController.
Scenario:
I have 3 views
RootViewController - Parent View
SportsViewController - Sub View 1
CricketViewController - Sub View 2
Both the subview have to be in FullScreenMode, so tab-bar or navigation bar cannot be used.
At first, Sub View 1 is loaded which is having some content and a DONE button on it.
Once user press DONE button then Sub View 1 has to be unloaded and RootViewController should load the Sub View 2.
Query
I have successfully displayed SubView 1 and when user taps on DONE then I can unload it. But I did n't get how should I notify the RootViewController from Sub View 1 that Sub View 1 has unloaded and now it should load the Sub View 2?
Thanks in Advance
Paras Mendiratta
I think the easiest solution here is to use UINavigationController and just hide the navigation bar. You can use -setNavigationBarHidden:animated: to hide (or show) the nav bar.
one way is to implement a method like - (void)loadSecondView which does all u want to do when first view is unloaded. and then int doneButtonClicked-method u call this method like this: [super loadSecondView]; and also remove first view from superview.
I assume that the screen with the "Done" button is some kind of login screen. You actually do not need all the view controllers you have defined to do what you want.
Instead you could do like this:
At application launch set CricketViewController as the root view controller.
Immediately let CricketViewController present `SportsViewController" as a modal view controller, no animation.
As far as the user is concerned the sports view controller is the starting point.
Dismiss the modal view controller on the Done button tap, giving the user the illusion of going to the next view.
view1 = [[View1 alloc] initWithNibName:#"View1" bundle:nil]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
view1 = navigationController1;
[window addSubview:view1.view];
[window makeKeyAndVisible];
This is general idea so please change according to your problem.
Here is my code where I tried to use delegate pattern.
The problem is the sub view 1 (videoPlayer) is not able to call the delegate methods. :(
ViewSwitcher.h - Root Controller
#class VideoPlayer; //Sub View 1
#class LandingPage; //Sub View 2
#protocol viewSwitcherDelegate
-(void)notifyViewSwitcher;
#end
#interface ViewSwitcher : UIViewController
{
id <viewSwitcherDelegate> delegate;
}
#property (nonatomic, retain) VideoPlayer *videoPlayer;
#property (nonatomic, retain) LandingPage *landingPage;
#property(assign) id <viewSwitcherDelegate> delegate;
-(void)loadSecondView;
-(void)delegateSetInSubView;
#end
ViewSwitcher.m - Implementation
#synthesize videoPlayer;
#synthesize landingPage;
//#synthesize delegate;
// 1.4 -> Declare the delegate constructor
- (id <viewSwitcherDelegate>)delegate
{
return delegate;
}
// 1.5 -> Declare the setDelegate method
- (void)setDelegate:(id <viewSwitcherDelegate>)v
{
delegate = v;
}
- (void)viewDidLoad
{
VideoPlayer *videoController = [[VideoPlayer alloc] initWithNibName:#"VideoPlayer" bundle:nil];
self.videoPlayer = videoController;
[self.view insertSubview:videoController.view atIndex:0];
[videoController release];
[super viewDidLoad];
}
-(void)loadSecondView
{
NSLog(#"Call for loading 2nd View");
}
VideoPlayer.h - SubView 1 (Movie Player)
#interface VideoPlayer : UIViewController <viewSwitcherDelegate>
{
ViewSwitcher *viewSwitcher;
MPMoviePlayerController *videoController;
}
#end
VideoPlayer.m - implementation
-(void)notifyViewSwitcher
{
NSLog(#"notifyViewSwitcher called.");
//Attempted to call the loadSecondView of ViewSwitcher
I tried calling delegate's method but in log nothing is printed.
[viewSwitcher loadSecondView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// Setting the delegate
viewSwitcher.delegate = self;
return self;
}
- (void)viewDidLoad
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"bumper.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoController = [[MPMoviePlayerController alloc] initWithContentURL:url];
videoController.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:videoController.view];
videoController.view.frame = CGRectMake(0, 0, 480, 320);
videoController.fullscreen = YES;
// Remove the status bar from this view.
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:UIStatusBarAnimationFade];
// TODO: This observer needs to be removed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateChange:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
// Play the video.
[videoController play];
[super viewDidLoad];
}
// Receives notification once movie is finished.
-(void)playbackStateChange:(NSNotification*)notification
{
// TODO: Switch the view.
NSLog(#"Notification = %#", notification);
[self notifyViewSwitcher];
}
Here is the LOG:-
2011-08-03 02:44:47.333 My Video Player[24016:207] Notification = NSConcreteNotification 0x5768280 {name = MPMoviePlayerPlaybackDidFinishNotification; object = <MPMoviePlayerController: 0x5740940>; userInfo = {
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 0;
}}
2011-08-03 02:44:47.337 My Video Player[24016:207] notifyViewSwitcher called.

viewDidLoad method not being called using presentModalDialog method

I am using presentModalDialog to show a viewcontroller's view however the viewDidLoad never gets called?!
Is there any method that is called where I can place my logic that fills and configures the view?
EDIT:
It's a bit difficult to put a small amount of code, you kind of need to see it all, but here goes:
I have 2 nibs and 2 view controllers (portrait-mainvc/landscape) which both inherit from 1 baseclass which has the logic and the iboutlets. This is to allow me to re-use code. When the orientation changes in the main controller, it switches between the 2 controllers (modal dialog) which in turn use their respective nib's however they all use the same base code to configure the UI items.
#implementation HomeViewControllerBase
- (void)configureBestSellItems
{
[self startRetrievingRegions];
// load all the images from our bundle and add them to the scroll view
// NSUInteger i;
for (int i = 0; i <= 150; i++)
{
NSString *imageName = #"tempImage.jpg";
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[self.bestSellScrollView addSubview:imageView];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureBestSellItems];
}
#end
#interface HomeViewController : HomeViewControllerBase
{
}
#interface HomeViewController_Landscape : HomeViewControllerBase
{
}
#implementation HomeViewController
//This works for portrait
- (void)viewDidLoad
{
[super viewDidLoad];
}
#end
#implementation HomeViewController_Landscape
//This does not work
- (void)viewDidLoad
{
[super viewDidLoad];
}
//This works as suggested
- (void)awakeFromNib
{
[super configureBestSellItems];
}
#end
You can try these in your viewcontroller:
-(void)awakeFromNib
{
[super awakeFromNib];
// your code here
}
or
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// your code here
}
However, you should try to find why viewDidLoad is never called. You can also check this
link.
You may have a custom function in there named presentModalDialog:, as searching apple's docs doesn't have that function. You're calling a custom function that loads a Xib.
I'm doing the same thing.
You want this:
[self presentModalViewController:controller...
ViewDidLoad is called by Controllers, if the awakeFromNib is being called, you're probably getting, by itself, the view.
It's a common problem in objective c:
you should review the flowing:
1-initiation of the view controller you should initialize it in the right way in app delegate.
2-review the file owner of the view it must be the right class which usually the same name of uiview.

Handling an external screen on the iPad

Ok, I think its possible I've misunderstood the correct way to implement an external screen on the iPad and it is causing me a lot of headaches.
Since this is a long post, what I'm trying to do is create and send a view to an external screen over VGA, and remove the screen once I'm done with it. I'm having retain count issues so can't get it to work.
I have a view controller that can be called up onto the iPad at any time. When this view loads (it is a remote, similar to Keynote presentation) I check for an external screen, then create a window and add a view to the extra monitor.
in my ipadViewController.h <-- the view that stays on the iPad
I have
#interface ipadViewController : UIViewController {
PresentationViewController *presentationView;
UIScreen *externalScreen;
UIWindow *externalWindow;
}
#property (nonatomic, retain) UIScreen *externalScreen;
#property (nonatomic, retain) UIWindow *externalWindow;
#property (nonatomic, retain) PresentationViewController *presentationView;
#end
(There is more, but that is the external screen stuff).
in ipadViewController.m:
#synthesize externalScreen;
#synthesize externalWindow;
#synthesize presentationView;
So I try to do a few things when the view loads:
Get the external screen (if possible)
Create apresentationViewController and add it to the extra screen
- (void)viewDidLoad {
[super viewDidLoad];
[self getExternalScreen];
[self createPresentationAndSendToWindow];
}
to get the screen I do the following getExternalScreen::
if ([[UIScreen screens] count] > 1)
{
for (UIScreen *currentScreen in [UIScreen screens])
{
if (currentScreen != [UIScreen mainScreen])
self.externalScreen = [currentScreen autorelease];
}
}
and to send the view to it createPresentationAndSendToWindow::
if (self.presentationPath == nil) return;
PresentationViewController *viewController = [[PresentationViewController alloc] initWithNibName:#"CanvasPresentation" bundle:nil];
self.presentationView = viewController;
[viewController release];
if (self.externalWindow == nil)
{
CGRect externalBounds = [self.externalScreen bounds];
self.externalWindow = [[[UIWindow alloc] initWithFrame:externalBounds] autorelease];
[self.externalWindow addSubview:self.presentationView.view];
self.externalWindow.screen = self.externalScreen;
[self.externalWindow makeKeyAndVisible];
}
in dealloc I try to cleanup with:
[presentationView release];
[externalScreen release];
//[externalWindow release]; <- that would crash
Problem I have is that when I dismiss the remoteViewController (it is modal), after releasing externalScreen has a retain count = 1 and externalWindow has retain count = 2.
The crash caused by externalWindow release disappears if I don't release presentationView (but then I'm leaking presentationView.
Your problem is here:
for (UIScreen *currentScreen in [UIScreen screens])
{
if (currentScreen != [UIScreen mainScreen])
self.externalScreen = [currentScreen autorelease];
}
Remove the autorelease. You shouldn't be releasing something you didn't create or retain.
you are autoreleasing externalView on
self.externalWindow = [[[UIWindow alloc] initWithFrame:externalBounds] autorelease];
and then you assign an autorelease view to it
self.externalScreen = [currentScreen autorelease];
you cannot release an autoreleased view, or it will crash.

"Swapping" a UIView Instance variable - cannot dealloc "previous" view

I want to organize somehow my iPhone game's level-views, but I simply cannot (without expanding Object Allocations). I made a really "skeleton" of my code (this game has 2 levels, the goal is to release the iPhone display). I just cannot dealloc the previous level, so Instrunments shows incrementing BGTangramLevel instances.
Please, take a look on it, I need some helpful ideas on designing (my 3rd question on it).
viewcontroller.h
#interface compactTangramViewController : UIViewController
{
//The level.
BGTangramLevel *currentLevel;
UIColor *levelColor;
}
//It is to be just a reference, therefore I use assign here.
#property (nonatomic, retain) BGTangramLevel *currentLevel;
-(void) notificationHandler: (NSNotification*) notification;
-(void) finishedCurrentLevel;
#end
viewcontroller.m
#implementation compactTangramViewController
#synthesize currentLevel;
//Initializer functions, setting up view hierarchy.
-(void) viewDidLoad
{
//Set up levelstepper.
levelColor = [UIColor greenColor];
//Set up "state" classes.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationHandler:) name:#"finishedCurrentLevel" object:nil];
//Attach level 1.
currentLevel = [BGTangramLevel levelWithColor: levelColor frame:self.view.frame];
[self.view addSubview:currentLevel];
[super viewDidLoad];
}
//Release objects.
-(void) dealloc
{
[currentLevel release];
[super dealloc];
}
//Notification handling.
-(void) notificationHandler: (NSNotification*) notification
{
//Execute level swap.
if ([notification name] == #"finishedCurrentLevel") [self finishedCurrentLevel];
}
-(void) finishedCurrentLevel
{
//Remove previous level.
[currentLevel removeFromSuperview];
//[currentLevel release];
//Step level.
if (levelColor == [UIColor greenColor]) levelColor = [UIColor blueColor]; else levelColor = [UIColor greenColor];
//Attach level 2.
currentLevel = [BGTangramLevel levelWithColor: levelColor frame:self.view.frame];
[self.view addSubview:currentLevel];
}
#end
BGTangramLevel.h
#interface BGTangramLevel : UIView
{
BOOL puzzleCompleted;
}
//Initializer.
+(BGTangramLevel*)levelWithColor: (UIColor*) color frame: (CGRect) frame;
//Test if the puzzle is completed.
-(void) isSolved;
#end
BGTangramLevel.m
#implementation BGTangramLevel
//Allocated instance.
+(BGTangramLevel*)levelWithColor: (UIColor*) color frame: (CGRect) frame
{
BGTangramLevel *allocatedLevel = [[BGTangramLevel alloc] initWithFrame:frame];
allocatedLevel.backgroundColor = color;
return allocatedLevel;
}
//Finger released.
-(void) touchesEnded: (NSSet*)touches withEvent: (UIEvent*)event
{
//The completement condition is a simple released tap for now...
puzzleCompleted = YES;
[self isSolved];
}
//Test if the puzzle is completed.
-(void) isSolved
{
//"Notify" viewController if puzzle has solved.
if (puzzleCompleted) [[NSNotificationCenter defaultCenter] postNotificationName:#"finishedCurrentLevel" object:nil];
}
-(void) dealloc
{
NSLog(#"Will ever level dealloc invoked."); //It is not.
[super dealloc];
}
#end
So what should I do? I tried to mark autorelease the returning level instance, release currentLevel after removeFromSuperview, tried currentLevel property synthesized in (nonatomic, assign) way, but Object Allocations still grow. May I avoid Notifications? I'm stuck.
You need to follow retain/release rules more closely. You definitely should not experimentally add retain and release and autorelease in places just to find something that works. There's plenty written about Cocoa memory management already, I won't repeat it here.
Specifically, BGTangramLevel's levelWithColor:frame: method should be calling [allocatedLevel autorelease] before returning allocatedLevel to its caller. It doesn't own the object, it's up to the caller to retain it.
You also need to know the difference between accessing an instance variable and accessing a property. Cocoa's properties are just syntactic-sugar for getter and setter methods. When you reference currentLevel in your view controller you are dealing with the instance variable directly. When you reference self.currentLevel you are dealing with the property.
Even though you've declared a property, currentLevel = [BGTangram ...] simply copies a reference into the variable. In viewDidLoad, you need to use self.currentLevel = [BGTangram ...] if you want to go through the property's setter method, which will retain the object (because you declared the property that way). See the difference?
I think your leak is happening in finishedCurrentLevel. If you had used self.currentLevel = [BGTangram ...], the property's setter method would be called, which would release the old object and retain the new one. Because you assign to the instance variable directly, you simply overwrite the reference to the old level without releasing it.
Calling [currentLevel release] in the dealloc method of your view controller is correct.

View loaded from nib will not update contents of UILabel while running in a method

I am having an issue with updating the contents of an "myInfoBox" object I created to be displayed while some background processes are done.
In the delegate method I am creating a new viewController:
-(void)loadMainView
{
myFirstViewController = [[MyFirstViewController alloc] initWithNibName:#"MyFirstView" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
// myFirstViewController was retained again by the controller, release one
[myFirstViewController release];
navigationController.navigationBar.hidden = YES;
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
// the next method is run after the "viewDidLoad" is finished loading
[myFirstViewController loadAlertViewForNewUser];
}
Following is my implementation of "myFirstViewController", it creates an instance of the "infoBox" class(I will show its code later):
- (void)viewDidLoad {
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 0, 0);
self.navigationController.navigationBar.bounds = CGRectMake(0, 0, 0, 0);
self.myInfoBox = [[InfoBoxController alloc] initWithNibName:#"InfoBox" bundle:[NSBundle mainBundle]];
CGRect infoBoxFrame;
infoBoxFrame = CGRectMake(60, 120, 200, 200);
myInfoBox.view.frame = infoBoxFrame;
myInfoBox.i_statusLabel.text = #"Downloading Account Updates";
myInfoBox.i_titleLabel.text = #"Updating";
// disabled for testing
//myInfoBox.view.hidden = YES;
[self.view addSubview:myInfoBox.view];
[super viewDidLoad];
}
// this method is called after the view has been loaded by the delegate
- (void)loadAlertViewForNewUser
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Welcome!" message:#"Connect to download stuff from your account?"
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alert.tag = 0;
[alert show];
}
// implementation of the alertview delegate
- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == 0)
{
if (buttonIndex == 0)
{ NSLog(#"button 0 was pressed"); }
if (buttonIndex == 1)
{
// this is the button that is pressed
[actionSheet removeFromSuperview];
[actionSheet release];
// tried using this also
//[self performSelectorOnMainThread:#selector(userInitialSetupMainThread) withObject:nil waitUntilDone:NO];
// do stuff and update the infobox about it
[self loadInfoBoxInitialUserSetup];
// tried using this as well
//[self performSelectorInBackground:#selector(loadInfoBoxInitialUserSetup) withObject:nil];
}
return;
}
}
- (void)loadInfoBoxInitialUserSetup
{
[self performSelectorOnMainThread:#selector(userInitialSetupMainThread) withObject:nil waitUntilDone:NO];
}
- (void)userInitialSetupMainThread
{
// fetch JSON data
NSDictionary *responseJSON = [NSDictionary dictionaryWithDictionary:[self getUserstuff]];
self.myInfoBox.i_statusLabel.text = #"Processing Recieved information";
// breakpoint - nothing changes in the view on the simulator
[myInfoBox.view setNeedsLayout];
// breakpoint - nothing changes in the view on the simulator
[myInfoBox.view setNeedsDisplay];
// breakpoint - nothing changes in the view on the simulator
[myInfoBox.parentViewController.view setNeedsLayout];
// breakpoint - nothing changes in the view on the simulator
[myInfoBox.parentViewController.view setNeedsDisplay];
// breakpoint - nothing changes in the view on the simulator
[myInfoBox performSelectorOnMainThread:#selector(updateValuesForTitle:) withObject:#"test" waitUntilDone:YES];
// breakpoint - nothing changes in the view on the simulator
[self.view setNeedsLayout];
// breakpoint - nothing changes in the view on the simulator
[self.view setNeedsDisplay];
// breakpoint - nothing changes in the view on the simulator
self.myInfoBox.i_statusLabel.text = #"Reloading...";
// breakpoint - nothing changes in the view on the simulator
[self readStuffFromDB];
sleep(2);
//disabled view removal for testing..
//[self.myInfoBox.view removeFromSuperview];
// breakpoint - nothing changes in the view on the simulator
}
What happens for me in the testing is that the myInfoBox object is created on screen when the -(void)loadMainView method is complete, then I can see on screen the "myInfoBox" in the background while the alertView in front (for testing...) at this point the screen is responsive and I can select the YES, once I select yes the delegate method is called.
As I commented in the source file, using breakpoints I am monitoring the simulator and following the code, never the less the changed label values are not reflected while I am still in the - (void)userInitialSetupMainThread method, but once it finishes the view updates with the latest set .text value!! grrr..
Also, the source for the myInfoBox class:
#interface InfoBoxController : UIViewController {
IBOutlet UILabel* i_titleLabel;
IBOutlet UILabel* i_statusLabel;
IBOutlet UIImageView* i_loadingImage;
IBOutlet UIImageView* i_background;
IBOutlet UIActivityIndicatorView* i_activityIndicator;
}
#property(nonatomic, retain) IBOutlet UILabel* i_titleLabel;
#property(nonatomic, retain) IBOutlet UILabel* i_statusLabel;
#property(nonatomic, retain) IBOutlet UIImageView* i_loadingImage;
#property(nonatomic, retain) IBOutlet UIImageView* i_background;
#property(nonatomic, retain) IBOutlet UIActivityIndicatorView* i_activityIndicator;
//- (void)updateValuesForTitle:(NSString *)title Label:(NSString *)label;
- (void)updateValuesForTitle:(NSString *)title;
#end
#implementation InfoBoxController
#synthesize i_titleLabel, i_statusLabel, i_loadingImage, i_background;
#synthesize i_activityIndicator;
//-(void)updateValuesForTitle:(NSString *)title Label:(NSString *)label
-(void)updateValuesForTitle:(NSString *)title
{
self.i_titleLabel.text = title;
self.i_statusLabel.text = title;
[self.i_titleLabel setNeedsDisplay];
[self.i_statusLabel setNeedsDisplay];
}
Sorry for the LOONG post :)
PLEASE ASSIST!
At the risk of sounding unhelpful, that's kind of just how it works. If you have long-running code in the main event loop (i.e., you don't explicitly create a thread or similar), the operating system won't be able to update the UI.
To update the UI while your code is running, you either need to run your complex operation in the back ground using thread, NSOperationQueue, etc, or just break it into smaller steps and return control to the main loop occasionally so that the UI can be updated.