Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In my app i have tab bar and in it there is three tab button. but on one of it when i pressed it second time it crash the app. Other button are working fine.
This is the .m file code
-(void)viewWillAppear:(BOOL)animated
{
scrollView.contentSize = CGSizeMake(320, 500);
[super viewWillAppear:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Contact Us", #"Contact Us");
self.tabBarItem.image = [UIImage imageNamed:#"contactTab.png"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
scrollView.contentSize = CGSizeMake(320, 500);
scrollView.scrollEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[textView becomeFirstResponder];
return YES;
}
- (void)keyboardWasShown:(NSNotification *)notification
{
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// Step 3: Scroll the target text field into view.
CGRect aRect = self.view.frame;
aRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) )
{
CGPoint scrollPoint = CGPointMake(0.0, self.activeTextField.frame.origin.y - (keyboardSize.height-15));
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void) keyboardWillHide:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
// Set activeTextField to the current active textfield
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
}
// Set activeTextField to nil
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeTextField = nil;
}
// Dismiss the keyboard
- (IBAction)dismissKeyboard:(id)sender
{
[self.activeTextField resignFirstResponder];
}
So which code is crashing my app?
Edited :
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tabBarController = [[UITabBarController alloc] init];
//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController
HomeViewController * homeViewController = [[HomeViewController alloc] init];
UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeViewController];
ContactUsFormViewController *contactUsFormViewController = [[ContactUsFormViewController alloc] init];
UINavigationController *contactNav = [[UINavigationController alloc] initWithRootViewController:contactUsFormViewController];
BookingFormViewController *bookingFormViewController = [[BookingFormViewController alloc] init];
UINavigationController *bookingNav = [[UINavigationController alloc] initWithRootViewController:bookingFormViewController];
NSArray* controllers = [NSArray arrayWithObjects:homeNav,contactNav,bookingNav, nil];
tabBarController.viewControllers = controllers;
[self.window setRootViewController:tabBarController];
sleep(2);
[self.window makeKeyAndVisible];
return YES;
}
Thanks.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
scrollView.contentSize = CGSizeMake(320, 500);
scrollView.scrollEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
scrollView.contentSize = CGSizeMake(320, 500);
[super viewWillAppear:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Contact Us", #"Contact Us");
self.tabBarItem.image = [UIImage imageNamed:#"contactTab.png"];
}
return self;
}
Related
I finished the development of my app but at this point I face random crashes when I try to save an image in core data.
Here is the console message:
CoreData: error: Serious application error. Exception was caught
during Core Data change processing. This is usually a bug within an
observer of NSManagedObjectContextObjectsDidChangeNotification.
-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x94b69b0 with userInfo (null)
The problem is that it is non deterministic. It happens more or less once out of 5 times.
Would anyone have an idea where I should look for ?
EDIT:
Here is the code of my class that seems to pose a problem. It happens sometimes when I click on the save button, calling -(IBAction)saveResultingImage:
#import "ResultViewController.h"
#interface ResultViewController ()
#property (nonatomic)BOOL resultSaved;
#end
#implementation ResultViewController
#pragma mark - IBActions
- (IBAction)saveResultingImage
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
_resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_resultSaved = [self saveOnCoreData];
[self performSegueWithIdentifier:#"BackToStartSegue" sender:self];
}
- (IBAction)discardWithoutSaving
{
[self performSegueWithIdentifier:#"BackToStartSegue" sender:self];
}
- (IBAction)restartDrawing:(id)sender
{
UIImage *backgroundImage = _subImage;
UIImageView *imageView =
[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
imageView.image = backgroundImage;
[self.view addSubview:imageView];
UIImage *maskImage = _maskImage;
CGRect maskViewRect = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
ImageMaskView *subView = [[ImageMaskView alloc] initWithFrame:maskViewRect image:maskImage];
[self.view addSubview:subView];
}
- (IBAction)showExplainVC
{
[self performSegueWithIdentifier:#"ShowInfoSegue" sender:self];
}
#pragma mark - Methods
- (BOOL)saveOnCoreData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newMeme =
[NSEntityDescription insertNewObjectForEntityForName:#"Meme" inManagedObjectContext:context];
NSData *toSaveMeme = UIImagePNGRepresentation(_resultImage);
[newMeme setValue:toSaveMeme forKey:#"image"];
NSError *error = nil;
if (![context save:&error]) {
DebugLog(#"Can't save: %# %#", error, [error localizedDescription]);
return NO;
}
return YES;
}
#pragma mark - View Controller LifeCycle
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([identifier isEqualToString:#"BackToStartSegue"]) {
return _resultSaved;
}
return _resultSaved;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
DebugLog(#"Called viewWillAppear");
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *discardBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Discard" style:UIBarButtonItemStylePlain target:self action:#selector(discardWithoutSaving)];
UIBarButtonItem *saveBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveResultingImage)];
UIBarButtonItem *infoBarButton =
[[UIBarButtonItem alloc] initWithTitle:#"How to" style:UIBarButtonItemStylePlain target:self action:#selector(showExplainVC)];
discardBarButtonItem.style = UIBarButtonItemStyleBordered;
self.navigationItem.rightBarButtonItems = #[saveBarButtonItem, discardBarButtonItem, infoBarButton];
self.view.backgroundColor = [UIColor blackColor];
[self restartDrawing:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Core Data Stack
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:#selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
#end
Thanks for showing interest :-)
I present one controller to play video:
[self presentModalViewController:movieController animated:YES];
and add an observer when finish palying:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedPlayback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
-(void) movieFinishedPlayback:(NSNotification*)notification{
NSLog(#"........movieFinishedPlayback....... \n ");
[self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
This my VideoDetailViewController:
#import "VideoDetailViewController.h"
#import "PSStackedView.h"
#import "YunMaoIpadAppDelegate.h"
#define IpadAppDelegate ((YunMaoIpadAppDelegate *)[[UIApplication sharedApplication] delegate])
#define IphoneAppDelegate ((YunMaoIosAppDelegate *)[[UIApplication sharedApplication] delegate])
#interface VideoDetailViewController ()
#end
#implementation VideoDetailViewController
#synthesize video, moviePlayer, collectionsArray;
-(id)initWithVideo:(Video *)theVideo
{
self = [super initWithNibName:#"VideoDetailViewController" bundle:nil];
if (self) {
self.video = theVideo;
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[moviePlayer pause];
//[self.navigationController setNavigationBarHidden:false animated:animated];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated;
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[super viewDidDisappear:animated];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
}
- (void) viewDidAppear:(BOOL)animated
{
//[moviePlayer play];
//[DejalBezelActivityView removeViewAnimated:YES];
[super viewDidAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(displayActivityView) withObject:self.moviePlayer.view afterDelay:0.1];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:video.link]];
[moviePlayer prepareToPlay];
//[moviePlayer setShouldAutoplay:NO];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
if([moviePlayer respondsToSelector:#selector(useApplicationAudioSession)])
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSLog(#"iPhone ios5.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 480.0f, 320.0f)];
//moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
}
else{
NSLog(#"iPad ios5.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
[moviePlayer.view setFrame:[IpadAppDelegate window].bounds];
}
}
else
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
NSLog(#"iPhone ios6.x");
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
}
if(result.height == 568)
{
//moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer.view setFrame:self.view.bounds];
}
}
else{
NSLog(#"ipad ios 6.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
//[moviePlayer.view setFrame:[IpadAppDelegate window].bounds];
}
}
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
- (BOOL)shouldAutorotate{
return NO;
}
/*
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationLandscapeRight;
}
*/
- (IBAction)displayActivityView
{
[DejalBezelActivityView activityViewForView:self.moviePlayer.view withLabel:#"节目正在下载中,请稍后..."].showNetworkActivityIndicator = NO;
}
- (void)removeActivityView;
{
[DejalBezelActivityView removeViewAnimated:YES];
[[self class] cancelPreviousPerformRequestsWithTarget:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This worked for me.
[self dismissViewControllerAnimated:NO completion:^{
[self.view removeFromSuperview];
}];
Hope it helps!
Before dismissing movieplayer do [moviePlayer stop]; and remove movieplayer.view from superview. Try dismissing after that. That might fix the issue.
I have a question. I want to display the user some content in a UIScrollView. I want to autoscroll the UIScrollView fast from bottom to top (like in the apple stores iPad). I tried to use DDAutoscrollview (If someone knows), but it doesn't work for me. Do have someone a solution for me to autoscroll a UIScrollView? Any code snippets would be nice.
.h
#interface Interface1 : UIViewController {
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *warnung;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x,
self.scrollView.contentSize.height -
self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
- (void)viewDidLoad {
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 420)];
[super viewDidLoad];
}
Thanks.
> THUMBS UP FOR THE AWNSER THAT WAS GIVEN BY TOBI!!!
Just use setContentOffset:animated:
UIScrollView *scrollView = ...;
CGPoint newOffset = scrollView.contentOffset;
newOffset.y = 0;
[scrollView setContentOffset:newOffset animated:YES];
Edit:
To use it like some kind of start animation you could do this in the scrollView's view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGPoint newOffset = self.scrollView.contentOffset;
newOffset.y = 0;
[self.scrollView setContentOffset:newOffset animated:YES];
}
Edit 2 / 3:
To make the scrolling happen slower, use this:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
CGPoint bottomOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
float scrollDuration = 4.0;
[UIView animateWithDuration:scrollDuration animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}];
}
I've successfully embedded a UITextView inside a UITableViewCell, and most of the table is working fine. However, when I enable editing on the UITextView and start editing, the table shifts up too far, obscuring the cursor. Is there a way to control how far the tableview scrolls? Manually scrolling the view to the cell works, but the view still scrolls up before scrolling back down and into view.
Here is an example of what I mean:
editing a UITextField - the field is neatly placed directly above the keyboard.
http://imageshack.us/photo/my-images/13/textfield.png/
editing a UITextView - the field is placed significantly above the keyboard removing the toolbar from the keyboard doesn't affect anything)
http://imageshack.us/photo/my-images/809/textview.png/
Here's all the code that is relevant to the UITextView in the UITableViewController object:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(keyboardWillShow:)
name: UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(keyboardWillHide:)
name: UIKeyboardWillHideNotification object:nil];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
viewTargetedForEditing = (UIView *)textView;
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
// shrink the textView to fit on-screen
originalTextViewFrame = textView.frame;
CGRect keyboardRect = [[keyboardUserinfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = keyboardRect.size.height;
CGRect newViewFrame = textView.frame;
CGFloat spaceAboveKeyboard = self.containerView.frame.size.height - keyboardHeight;
newViewFrame.size.height = (spaceAboveKeyboard < textView.frame.size.height) ? spaceAboveKeyboard : textView.frame.size.height;
/* animate the calculations */
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[[keyboardUserinfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
textView.frame = newViewFrame;
[UIView commitAnimations];
// recalculate the keyboard height, in case we aren't in portrait mode
CGFloat toolbarHeight;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
toolbarHeight = 44.0;
} else {
toolbarHeight = 32.0;
}
CGRect frame = textView.inputAccessoryView.frame;
frame.size.height = toolbarHeight;
}
- (void)endTextViewEditing
{
[viewTargetedForEditing resignFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
#pragma mark - Keyboard Notifications
- (void)keyboardWillShow:(NSNotification *)notification
{
[keyboardUserinfo release];
keyboardUserinfo = [[notification userInfo] retain];
[self viewDidBeginEditing:viewTargetedForEditing];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[keyboardUserinfo release];
keyboardUserinfo = [[notification userInfo] retain];
[self viewDidEndEditing:viewTargetedForEditing];
}
- (void)viewDidBeginEditing:(id)aView
{
// called 2nd
UITableViewCell *cell = (UITableViewCell *)[aView superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)viewDidEndEditing:(id)aView
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[viewTargetedForEditing superview]];
gallupAppDelegate *appDelegate = (gallupAppDelegate *)[[UIApplication sharedApplication] delegate];
switch (indexPath.section) {
case SectionDescription:
if (![[(UITextField *)aView text] isEqualToString:self.plan.Description]) {
self.plan.Description = [(UITextField *)aView text];
[appDelegate saveManagedContext];
}
break;
case SectionNotes:
if (![[(UITextView *)aView text] isEqualToString:self.plan.Notes]) {
self.plan.Notes = [(UITextView *)aView text];
[appDelegate saveManagedContext];
}
break;
case SectionLocation:
if (![[(UITextField *)aView text] isEqualToString:self.plan.Location]) {
self.plan.Location = [(UITextField *)aView text];
[appDelegate saveManagedContext];
}
break;
}
}
The TextViewCell is a subclass of UITableViewCell, and it is the cell that is giving me the problem with scrolling.Here's the implementation:
#implementation TextViewCell
#synthesize contents=_contents;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_contents = [[UITextView alloc] initWithFrame:CGRectZero];
[self addSubview:_contents];
self.contents.font = [UIFont systemFontOfSize:17];
self.contents.dataDetectorTypes = UIDataDetectorTypeAll;
CGRect frame = CGRectMake(0, 0, self.window.frame.size.width, 44.0);
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(endTextViewEditing)];
UIToolbar *textViewToolbar = [[UIToolbar alloc] initWithFrame:frame];
textViewToolbar.barStyle = UIBarStyleBlackOpaque;
[textViewToolbar setItems:[NSArray arrayWithObjects:spacer, done, nil]];
self.contents.inputAccessoryView = textViewToolbar;
}
return self;
}
- (void)endTextViewEditing
{
[self.contents resignFirstResponder];
}
#define LEFT_MARGIN 15
#define TOP_MARGIN 5
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.frame.size.width - (LEFT_MARGIN *2);
CGFloat height = self.frame.size.height - (TOP_MARGIN *2);
self.contents.frame = CGRectMake(LEFT_MARGIN, TOP_MARGIN, width, height);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
//[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[super dealloc];
}
#end
As it turns out, subclassing the UITableView and implementing the following fixed the problem:
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
{
[super setContentOffset:contentOffset animated:animated];
return;
static int i = 0;
if (0 == i) {
i++;
} else {
i = 0;
[super setContentOffset:contentOffset animated:animated];
}
return;
}
Apparently the -setContentOffset:animated: message was sent 2x, and if the first one is allowed to run the second time round doesn't do it's job properly.
Does anyone have any insite as to why this would fix the problem?
I'm trying to acheive a dual-tabBar app for the iPhone and using the following code for a base class view controller to add several navigation controllers inside the view controller (see code below). But the problem is: No subviews are added to self.view, despite them being initialized earlier. Any ideas?
- (IBAction)ViewButtonPressed:(id)sender
{
UIButton *b = (UIButton *)sender;
int index = b.tag - 1000;
[self SelectNavigationController:index];
}
- (void)SelectNavigationController:(int)index
{
// Set index to top-most view ->
UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:index];
[self.view bringSubviewToFront:nc.view];
}
#pragma mark -
#pragma mark display
- (void)Display
{
CGRect frame = CGRectMake(0, 44, 320, 367);
// Create buttons above frame and show navigation controller inside frame ->
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.origin.y)];
for (int i=0; i<[navigationControllers count]; ++i)
{
UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:i];
UIViewController *vc = [nc.viewControllers objectAtIndex:0];
NSString *titel = vc.navigationItem.title;
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setBackgroundColor:[UIColor lightGrayColor]]; // TODO: Replace with image <-
[b setTitle:titel forState:UIControlStateNormal];
b.tag = i + 1000;
[b setFrame:CGRectMake(i * frame.size.width / 3, 0, frame.size.width / 3, frame.origin.y - 1)];
[v addSubview:b];
}
for (int j=0; j<[navigationControllers count]; ++j)
{
UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:j];
[nc.navigationBar addSubview:v];
[self.view addSubview:nc.view]; // Add view to view <-
nc.view.frame = frame;
}
[v release];
if (VIEW_DEBUG)
NSLog(#"BaseTabViewController.m: self.view.subviews: %d", [self.view.subviews count]);
}
#pragma mark -
#pragma mark addviewcontroller
- (void)AddViewControllerForNavigationController:(UIViewController *)viewController
{
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.view.backgroundColor = [UIColor greenColor];
[navigationControllers addObject:navController];
[navController release];
}
#pragma mark -
#pragma mark init, loadView, viewDidLoad and dealloc
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
navigationControllers = [[NSMutableArray alloc] init];
}
return self;
}
- (void)loadView
{
//
}
- (void)viewDidLoad
{
if (!viewDidLoadAlready)
{
[self Display];
viewDidLoadAlready = YES;
[super viewDidLoad];
}
}
And the code in the subclass:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
PistKartaViewController *pistKarta = [[PistKartaViewController alloc] init];
pistKarta.navigationItem.title = #"Pistkarta";
LiftRapportViewController *liftRapport = [[LiftRapportViewController alloc] init];
liftRapport.navigationItem.title = #"Liftrapport";
SkipassViewController *skiPass = [[SkipassViewController alloc] init];
skiPass.navigationItem.title = #"Skipass";
[self AddViewControllerForNavigationController:pistKarta];
[self AddViewControllerForNavigationController:liftRapport];
[self AddViewControllerForNavigationController:skiPass];
[pistKarta release];
[liftRapport release];
[skiPass release];
}
return self;
}
I figured it out. I had the following in a view controller I was adding...
- (void)loadView
{
}
, which of course means that the superclass won't be loaded at all. Stupid. Otherwise, this method works quite well. :)