Reputation: 492
I have information in a Master View which uses a UITableView object and a detail ViewContrailer that comes up after I push a user selection to it.
My challenge is with scrolling in a UITextView object, that doesn't go back to the top of the scrolling area on the detail View.
The user chooses a detail object from a list in the Master View Controller, then , brings it up in the detail ViewController. She then scrolls within the UITextView object - dogScrollingInfoTextView- on that view, going farther down so that upper lines don't show anymore. She then return to the Master View Controller, and selects a different row. Upon returning to the detail view, the new object's UITextView object (dogScrollingInfoTextView) is still positioned where the previous view left it.
The object being passed into this UITextView in the detailViewController, looks like this in the Master View Controller
self.detailViewController.dogScrollingInfo = dog.whatMakesDogSpecial;
In the original class definition, this property is declared like this
@property (strong, nonatomic) NSString *whatMakesDogSpecial;
The object used to transfer within the detailView controller class to the view are declared like this
@property (strong, nonatomic) NSString *dogScrollingInfo;
@property (weak, nonatomic) IBOutlet UITextView *dogScrollingInfoTextView;
and in the detailViewController implementation the transfer looks like this
self.dogScrollingInfoTextView.text = [self.dogScrollingInfo description];
I'm not sure if I should attach screen shots ? Should I put in any other code? (I have put two methods below, one from each view controller class)
I can't find anything specifically related to scrolling in the UITextView Class or the UIView class, that tells me I have to do something programatically. This seems like it may have something to do with InterfaceBuilder. I'm using xib's NOT storyboards. But I can't find any choices in the different Inspectors that change this behavior.
Is configureView, which I use in the detail view controller class, perhaps not the right method to use for this? I think I found an example using the configureView method in the Xcode Master-Detail project template, though I built my application up from a Single View project template. And if not, how would I figure out what method to use?
I'm trying to learn to work with the apple documentation to solve challenges like this, but I find it hard navigating it. So pointers as to how to use the documentation better are much appreciated.
.... Code Snip from the Master View Controller class...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[HardlyWorkingDogsDetailViewController alloc]
initWithNibName:@"HardlyWorkingDogsDetailViewController"
bundle:nil ];
}
NSLog(@"The user selected dog @array position %d", indexPath.row);
Dog *dog = self.sortedDogDictionaryArray[indexPath.row];
self.detailViewController.dogName= dog.dogName;
self.detailViewController.dogLicense = dog.licenseString;
self.detailViewController.dogScrollingInfo = dog.whatMakesDogSpecial;
self.detailViewController.dogPhoto = dog.dogPhoto;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
.... Code Snip from the Detail View Controller class...
-(void)configureView {
//NSLog(@"ConfigureView # 1 in detail vu");
if (self.dogLicense) {
self.dogLicenseUILabel.text = [self.dogLicense description];
self.dogNameUIText.text = [self.dogName description];
self.dogScrollingInfoTextView.text = [self.dogScrollingInfo description];
self.dogPhotoUIImage.image = self.dogPhoto;
}
}
Thank you Laurel
Upvotes: 1
Views: 162
Reputation: 3131
Since you are reusing a HardlyWorkingDogsDetailViewController
, this behavior makes sense. If HardlyWorkingDogsDetailViewController
is light weight enough, there really isn't any need to reuse it. Simply reinstantiating it everytime should fix your problem:
self.detailViewController = [[HardlyWorkingDogsDetailViewController alloc] initWithNibName:@"HardlyWorkingDogsDetailViewController" bundle:nil ];
Dog *dog = self.sortedDogDictionaryArray[indexPath.row];
self.detailViewController.dogName= dog.dogName;
self.detailViewController.dogLicense = dog.licenseString;
self.detailViewController.dogScrollingInfo = dog.whatMakesDogSpecial;
self.detailViewController.dogPhoto = dog.dogPhoto;
[self.navigationController pushViewController:self.detailViewController animated:YES];
Although, having not seen your other logic, I can't say that won't introduce other bugs if any of your code depends on self.detailViewController
only being created once (keeping a reference to it elsewhere, for example). If that is the case, or you just don't want to change the MasterViewController code, you can simple put the following in HardlyWorkingDogsDetailViewController
:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.dogScrollingInfoTextView setContentOffset:CGPointMake(0, 0) animated:NO];
}
This makes it so every time the view is about to appear on the screen, the scroll view will get set back to the top. (viewDidLoad
, viewWillAppear:
, viewDidAppear:
, viewWillDisappear:
, viewDidDisappear:
are part of UIViewController
lifecycle and are very useful. You should read up on them)
On a side note, UITextView
is implemented using a UIScrollView
, so any scrolling method a UIScrollView
has, the UITextView
can do as well. More info here
Upvotes: 1