I'm having a problem similar to this... Why am I seeing a black screen? Did I not alloc something?
Sorry for the beginner question but I'm not sure how to debug this. I'm seeing a blank (black) screen for one of my view controllers. When I set the view controller to "is initial view controller" in the inspector, it works as expected. However when I remove that (it shouldn't be the initial VC, just did that to debug) and navigate to the view, I get a blank (black) screen.
There is a webview in the VC (referenceDetailWebView) and when I try to log that out on the viewDidLoad of the VC and I get (null). Again, when I set the VC to be "initial", it logs out the referenceDetailWebView object as expected.
This is the implementation of the VC...
#implementation DetailViewController
#synthesize currentReference, referenceDetailWebView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"reference/accommodation" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[referenceDetailWebView loadHTMLString:htmlString baseURL:nil];
// log for debugging
NSLog(#"%#", [self referenceDetailWebView]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The only thing that I can think of is that I might be missing something in my segues. I'm handling them programmatically and it's very possible that I'm overlooking something there...? My didSelectRowAtIndexPath looks like this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Reference *currentItem = [references objectAtIndex:indexPath.row];
NSString *mySegueID;
if([currentItem hasSubReferences]){
mySegueID = #"ShowSubTable";
} else {
mySegueID = #"ShowDetail";
}
[self performSegueWithIdentifier:mySegueID sender:nil];
}
And my performSegueWithIdentifier looks like this...
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
// the segue with identifier allows us to chose a segue programatically
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
if([identifier isEqualToString:#"ShowSubTable"]){
// looking for sub table
ReferencesSubTableViewController *subTableVC = [[ReferencesSubTableViewController alloc] initWithNibName:nil bundle:nil];
[subTableVC setCurrentReference:item];
[self.navigationController pushViewController:subTableVC animated:YES];
} else if ([identifier isEqualToString:#"ShowDetail"]){
// looking for detail view
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[detailVC setCurrentReference:item];
[self.navigationController pushViewController:detailVC animated:YES];
}
}
The segues are working as expected with the exception of that detailViewController displaying a blank screen and giving me null for my referenceDetailWebView.
I'm stuck. Any advice or guidance is very much appreciated! Thanks in advance for the help!!
Edit: It looks like the segues are probably the cause. When I remove the multiple segues and related code, and add a simple segue from the table cell to the DetailViewController only, the detailVC and views load correctly. Not sure what I'm missing in the segues though...?
I've simplified the performSegueWithIdentifier to the following and I'm still getting the blank screen...
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[detailVC setCurrentReference:item];
[detailVC setTitle:[item title]];
[self.navigationController pushViewController:detailVC animated:YES];
}
You're getting a black screen because you're instantiating your controller incorrectly. Also, you don't push the view controller with pushViewController:animated:, that's done for you when you call performSegueWithIdentifier:. If you have the segue setup in the storyboard, then you should be implementing prepareForSegue:sender:, not performSegueWithIdentifier:. Its code should look like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// the segue with identifier allows us to chose a segue programatically
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Reference *item = [references objectAtIndex:path.row];
if([segue.identifier isEqualToString:#"ShowSubTable"]){
// looking for sub table
ReferencesSubTableViewController *subTableVC = (ReferencesSubTableViewController *)segue.destinationViewController;
[subTableVC setCurrentReference:item];
} else if ([segue.identifier isEqualToString:#"ShowDetail"]){
// looking for detail view
DetailViewController *detailVC = (DetailViewController *)segue.destinationViewController;
[detailVC setCurrentReference:item];
}
}
You really need to read and understand Apple's docs on view controllers and segues -- you clearly have some fundamental misunderstanding of the way they work.
Related
I have a Master Detail Application, on the RootViewController(master) I'm using a tableview to display links, when a user clicks on one of the cells it should populate the detailview with the appropriate webpage.
In the RootViewController here is the code I'm using on the DidSelectRow:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"did select row");
NSArray* reversedArray = [[objectData reverseObjectEnumerator] allObjects];
NSDictionary *snapshot = [[NSDictionary alloc] init];
snapshot = [reversedArray objectAtIndex:indexPath.row];
DetailViewController *myCont = [[DetailViewController alloc] init];
NSString *url = snapshot[#"url"];
[myCont loadPage:url];
}
On the detailview here is the method I have to load the webpage
- (void)loadPage:(NSString*)urlString
{
NSLog(#"THE URL: %#",urlString);
NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSLog(#"REQUEST: %#",request);
//self.myWebView.scalesPageToFit = YES;
[myWebView loadRequest:request];
}
In my Storyboard, I have my UIWebView connected to my DetailViewController for webview delegate and webview outlet.
The page never loads
Have you tried conforming to the UIWebViewDelegate protocol?
UIWebViewDelegate has these callbacks that will help you debug:
webView:shouldStartLoadWithRequest:navigationType:
webViewDidStartLoad:
webViewDidFinishLoad:
webView:didFailLoadWithError:
All you need to do is find the identifier for the segue and pass the URL string to the DetailViewController when trying to segue into that controller and actually call your loadPage method inside the viewDidLoad() of DetailViewController.
In the Master-Detail template apple provides you can pass data like this
** Take note of the [controller setDetailItem: object] thats where you should pass your URL.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}
Your are creating a new DetailViewController, instead of take the DetailViewController of your storyboard.
This line is wrong:
DetailViewController *myCont = [[DetailViewController alloc] init]
"In my Storyboard, I have my UIWebView connected to my
DetailViewController for webview delegate and webview outlet."
But in your code it seems like you create Details View Controller manually, not from storyboard.
DetailViewController *myCont = [[DetailViewController alloc] init];
You should use UIStoryboardSegue instead of alloc+init.
Try to read this article - https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingViewControllersinYourApplication/UsingViewControllersinYourApplication.html
I'm trying to pass data(_claimReportToDetailView) from viewDidLoad (of MasterVC) to DetailVC. It's always null.
#interface LAMasterViewController ()
{
NSArray *_claimReports;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_claimReports = [[LADataModelController getSingleton] getClaimReportsOrderedByAccessedDate];
LADetailViewController *detailViewController = [[LADetailViewController alloc] init];
detailViewController.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
NSLog(#"claim%#",detailViewController.claimReportToDetailView); // captures here properly.
}
#interface LADetailViewController : UIViewController
#property(nonatomic ) LAClaimReport *claimReportToDetailView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"sdfdf%#", _claimReportToDetailView); // logs null always.
}
Your viewDidLoad seems strange. You have this line:
LADetailViewController *detailViewController = [[LADetailViewController alloc]init];
Yet you say that the view controller is on the storyboard.
I think what is happening is that you are creating this VC, and setting it's property, but the Storyboard is loading a completely new VC, for which you haven't set the property.
Usually, the way you pass information to VCs on Storyboards is in the prepareForSegue: method.
You have to pass the data to the details view controller in prepareForSeque method in master view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) { //<-make shire the segue identifier match one in storyboard
_claimReports = [[LADataModelController getSingleton] getClaimReportsOrderedByAccessedDate] ;
LADetailViewController *vc = (LADetailViewController*)[sender destinationViewController];
vc.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
}
}
This should fix your project. Give it a try
//Allocating LADetailViewController instance
LADetailViewController *detailViewController = [[LADetailViewController alloc]init];
//Connecting it the specified VC on storyboard
detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"yourVCID"];
//Now the connection is set, so pass the data you need
detailViewController.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
//Depending on present or push you should try one of the 2 lines
[self.navigationController pushViewController:detailViewController animated:YES];
//or
[self presentViewController:detailViewController animated:YES completion:nil];
This happens because whenever you navigate from one UIViewController to other, it is initialized again, so rather than setting value in viewDidLoad, try to set value on the event when you perform navigation and make navigation through code rather than with segue.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog((#"This is didSelectRowAtIndexPAth"));
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
/* The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content*/
NSLog(#"My Dictionary: This is a MasterView%#",detailViewController.myDictionary);
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
It is because the presented instance of your DetailViewController is different than the instance you are creating manually. You should either present that new ViewController yourself (modally or by push), or implement the prepareForSegue method and set the dictionary there.
Edit:
The 2 solutions:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
// If it is a modal
[self presentViewController:detailViewController animated:YES];
}
OR
#implementation MyClass {
NSIndexPath *selectedCellIndexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog((#"This is didSelectRowAtIndexPAth"));
selectedCellIndexPath = indexPath;
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetail"]) {
DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
detailViewController.myDictionary = [self.placeArray objectAtIndex:selectedCellIndexPath.row];
}
}
try this
Decalre one NSInteger varable Globaly assign that varble to indexpath.row
#interface DashbordViewController ()
{
NSInteger SelectedRownum;
}
#end
#implementation DashbordViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SelectedRownum=indexPath.row
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"])
{
NSLog((#"This is didSelectRowAtIndexPAth%#",SelectedRownum));
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.myDictionary = [self.placeArray objectAtIndex:SelectedRownum];
}
}
In DetailViewController check if the myDictionary is a property declared as weak or strong..
try this . . .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog((#"This is didSelectRowAtIndexPAth"));
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"myStoryBoardName" bundle:nil];
self.detailsViewController = [mystoryboard instantiateViewControllerWithIdentifier:#"detailviewcontroller"]
/* The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content*/
NSLog(#"My Dictionary: This is a MasterView%#",detailViewController.myDictionary);
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
you have to set identifier of your detailViewController in storyBoard .
You need to assign the dictionary in prepareForSegue: method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *viewController = = (DetailViewController *)[segue destinationViewController];
detailViewController.myDictionary = //YOUR Dictionary here;
}
This way the same dictionary can be obtained in viewDidLoad: of DetailViewController.
Note: Make sure you allocate the dictionary.
Hope it helps.
Since I can't see implementation of your controller (being subclass of UIViewController or not), I cannot say a lot, but keep in mind that initWithNibName:bundle: is the designated initializer for UIViewController and not init.
So, this is the first problem. In any case, check the instance before setting a property. It is very likely that instance is nil.
EDIT:
It is also very likely, they you are instantiating new View Controller from existing. In this case, you can add new view controller as child controller or present new controller from existing. In both cases properties of existing are reachable from new through properties: parentViewController and presentingViewController, which makes the story opposite: you take property value from existing controller into new controller.
You are creating new view controller in didSelectRowAtIndexPath, and at the end you are calling prepareForSegue method... which will create new Instance, So you have to set your data which you want to send in new view controller in prepareForSegue method like this.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//
if ([[segue identifier] isEqualToString:#"name_of_ur_segue"])
{
// Get reference to the destination view controller
DetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// Pass any data here ...
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
}
}
Edit:
As you are accessing data in ViewDidLoad method and in iOS 7 data passed like this is not accessible in ViewDidLoad method. As view controller presentation logic is bit changed now. Now we can pass data in initMethod if we want to access this in viewDidLoad.
Or access this in viewWillAppear or viewDidApepar.
If you want to call this code only once in viewDidAppear or ViewWillApepar just set a boolean flag, which can tell if you want to access this or not.
I am trying to use Table Views and navigation controllers. This is the code I would use if I was using NIB files. This piece of code would work fine if I was using NIB files, but I decided to try storyboard -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
I have created another scene in storyboard and have everything set up out there. When a user clicks a cell in this Table View they will be taken to that scene. This is what I wrote for the code but it is not right and I'm hopelessly stuck :( -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"articleView"]){
UINavigationController *navigationController = segue.destinationViewController;
WebViewController *controller = (WebViewController *)navigationController.topViewController;
controller.webView = self;
}
Answer - Answers 2 and 3 are both right
I had a very similar problem 2 days ago, with the user selecting the disclosure accessory on a row and wanting to invoke a segue. What worked for me:
In storyboard, create segue "articleView" from the entire tableView controller (not the cell) to the next viewController.
Add the following method to your tableViewController:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"articleView" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}
Just for you I also tested with tableView:didSelectRowAtIndexPath: and it works.
Enjoy,
Damien
If your table view is already embedded in a navigation controller (looks that way, from your first code snippet) then the destination view controller of your segue will be a WebViewController, not a navigation controller.
You can therefore have your prepareForSegue method pass the item directly across :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"articleView"])
{
WebViewController *controller = (WebViewController *)segue.destinationViewController;
RSSEntry *entry = [_allEntries objectAtIndex:[self.tableView indexPathForSelectedRow].row];
controller.entry = entry;
}
}
You may find this excellent two-part tutorial helpful
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
I'm working sample app by storyboard.
When made popoverview, I used the way - 'Embed in navigation controller'.
But I'm getting a big trouble of delegate usage.
It's that don't delegate to popover view's.
[ViweController.m]
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"PopRootViewController"]){
NSLog(#"[segue destinationViewController] :%#", [segue destinationViewController]);
rootViewController = [[RootViewController alloc] init];
rootViewController.delegate = (id)self;
NSLog(#"%#---%#---%#", rootViewController.delegate, self, rootViewController);
}
}
-(void)didTap22 {
NSLog(#"delegate step 1 success!! ");
}
The result of this source like this :
[segue destinationViewController] :<UINavigationController: 0x88660a0>
<ViewController: 0x6b795e0>---<ViewController: 0x6b795e0>---<RootViewController: 0x6b7da60>
but [RootViewController] don't receive the delegation.
[RootViewController.m]
#implementation RootViewController
#synthesize items, delegate;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
....
self.items = mutableFetchResults;
NSLog(#"333333..... %#, ....... %#", self.delegate, self);
}
results :
333333..... (null), ....... <RootViewController: 0x8866520>
RootViewController's delegation is null.
I can't find solution about this.
Anybody help me, please!
I suspect you didn't declare your delegate UIPopoverControllerDelegate in the class declaration of your view controller.
This line is suspicious:
rootViewController.delegate = (id)self;
you don't need to cast self normally if you made the declaration I am mentioning.