UITableView inside popover will not scroll - ios

I am trying to fix someone's code and am running into a problem. Here is the situation:
I have a full screen UIViewController that has a bottom bar button. When I press that button, it shows a popup view controller in the bottom left of the screen. That view controller inside the popup also has a button, and when I press THAT button it pushes a new view controller into my popup window. That new view controller is a UITableView that can have some large number of elements. The problem is that when I try to scroll down to see some offscreen elements, the scroll bounces back to the first position in the table, i.e. it does not really scroll.
More specifically here is some code:
In FullScreenVC.h:
#property (nonatomic, retain) NSMutableArray* stuffInList;
#property (nonatomic, retain) UIPopoverController *namePopover;
#property (nonatomic, retain) UINavigationController *nameNav;
#property (nonatomic, retain) UIBarButtonItem* addButton;
Inside FullScreenVC buttonPressed (called when addButton is pressed):
FirstPopupVC *vc=[FirstPopupVC alloc] initWithNibName#"FirstPopupVC" bundle:nil];
vc.delegate=self;
vc.stuffInList=self.stuffInList; // This is just the NSArray of list items
self.nameNav = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
self.namePopover = [[[UIPopoverController alloc]
initWithContentViewController:self.nameNav] autorelease];
[vc release];
[self.namePopover presentPopoverFromBarButtonItem:self.addButton
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
In other words, the FirstPopupVC object vc is the root view controller of a UINavigationViewController nameNav, and nameNav is the navigationcontroller that is the content inside the namePopover UIPopoverController. I think the last line launches FirstPopupVC as a popup from the addButton. Note also that the XIB file FirstPopupVC.xib is a simple NIB file that has a few simple views (label etc) and a little button that a user can press to get the second popup, as we'll see.
Inside FirstPopupVC we have:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// some other irrelevant initialization stuff here ...
self.contentSizeForViewInPopover = CGSizeMake(320, 340);
}
return self;
}
-(void)littleButtonClicked:(id)sender {
SecondPopupVC * secondVC=[[SecondPopupVC alloc]
initWithNibName:"#SimpleTableView" bundle"nil];
secondVC.delegate=self;
secondVC.stuffInList=self.stuffInList;
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
This of course presents the second view controller inside the popop.
Inside SecondPopupVC we have:
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.contentSizeForViewInPopover = CGSizeMake(320, 340);
}
return self;
}
Apparently the contentSizeForViewInPopover call here will set the size of the popover view with the table view. It looks like this is called inside initWithNibName in both the first and second view controllers.
So again, the problem is that if my listOfStuff is large, ie if the set of items in the table is large, then the table will not stay scrolled beyond the first visible set of rows. I can scroll down to see these items but as soon as I release the mouse pointer (on the emulator) or my finger (on the device), it bounces back to the top of the list.
I tried to add autoresizingmak code as discussed in table view in popover is not scrolling but that didn't work for me.
So how can I fix the above code so that my table stays scrolled when I try to scroll it?

Well, it appears that the problem has something to do with the fact that secondVC is created from a NIB file rather than programmatically. I tried doing this instead:
-(void)littleButtonClicked:(id)sender {
SecondPopupVC * secondVC=[[SecondPopupVC alloc] init];
CGSize contentSize = CGSizeMake(320, 320);
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
contentSize.width, contentSize.height)];
popoverView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
UITableView *tblView = [[UITableView alloc]initWithFrame:popoverView.bounds];
tblView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
tblView.delegate = secondVC;
tblView.dataSource = secondVC;
tblView.rowHeight = 44;
[popoverView addSubview:tblView];
secondVC.view = popoverView;
secondVC.contentSizeForViewInPopover = contentSize;
secondVC.stuffInList=self.stuffInList;
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
and guess what? It works fine. So why would there be such a problem when SecondPopupVC is initialized from a NIB file rather than using code?

Related

Unable to see the label in the view Controller

I have a view controller called "Help". I placed a label on the view controller story board, but in simulation I couldn't see the label. I used REMenu to put a menu at the top of the view. I can see the menu bar, but I cannot see the label. Here are the files.
#import "Help.h"
#interface Help ()
#end
#implementation Help
#synthesize helpText1;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack:)];
helpText1.text=#"Hiii";
helpText1.hidden=NO;
// Do any additional setup after loading the view.
}
-(void)handleBack:(id)otherView{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *myVC = (Controller *)[storyboard instantiateViewControllerWithIdentifier:#"QRList"];
[self.navigationController pushViewController:myVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the code that calls the "Help" view controller.When we click on the help option in the menu, it will call the "help" view controller.
REMenuItem *helpItem = [[REMenuItem alloc] initWithTitle:#"Help"
subtitle:nil
image:[UIImage imageNamed:#"Ic"]
highlightedImage:nil
action:^(REMenuItem *item) {
NSLog(#"Item: %#", item);
Help *controller = [[Help alloc] init];
[weakSelf setViewControllers:#[controller] animated:NO];
}];
The "Help" view controller is getting called but I couldn't see the label which I dragged on storyboard. I couldn't figure out the issue.
I guess, that your label is being at a wrong view. Probably, your pushed controller doesn't contain the label you put in your nib file. Try to add other objects and see what's happening. If the problem is that I just guessed, then find the exact controller, choose a view and put there the label. Hope you solve that!
Use debug, put breakpoints, to see where you lose label. Check your layouts, maybe...tie your lable to top Space to superView, leading or tralling space to supeView or bottom space to superView. And beginning Xcode 4.4 we don't need synthesize, each added default

Modal UIViewController appears blank

I'm appending code to an existing app to add a modal loginView, the old project does not use xib, and the new view controller does.
The present project load its rootViewController this way
- (void) loadView
{
// this should take up the entire screen...
UIView * view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor = [UIColor blueColor];
self.view = view;
sideBar =
[[SideBarView alloc] init];
sideBar.rootViewController = self;
[self.view addSubview:sideBar];
pageView =
[[PageView alloc] initWithTitle:DataEntryTitle
client:client];
pageView.rootViewController = self;
//I added this to instantiate the new view controller
_aLoginView = [[LoginViewController alloc]initWithUser:aUser];
[self.view addSubview:pageView];
}
To launch the modal view controller I'm using this code.
-(void)viewDidAppear:(BOOL)animated{
_aLoginView.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:_aLoginView animated:YES completion:nil];
}
As long as I understand the reason for the modal LoginView appearing blank is that I'm creating a new instance of it, instead of calling the existing xib definition. Does someone know what should I do to reference the xib? The old project does not use storyboard, nor xib files.
Thanks,
With your info I think I'm doing something wrong in LoginViewController.m, but I don't know exactly what.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithUser:(verifyUser *)aUser
{
self = [super initWithNibName:#"LoginViewController" bundle:[NSBundle mainBundle]];
if (self) {
// Custom initialization
_UserModel = aUser;
}
return self;
}
You want to create the view controller by loading it from the nib
[[LoginViewController alloc] initWithNib:#"nib file name" bundle:[NSBundle mainBundle]]];
If you want to keep your initWithUser: initializer you should be able to do
self = [super initWithNib:#"nib file name" bundle:[NSBundle mainBundle]];
The files owner of your xib is not set.
Open your xib and select the file owner option in the top left
Then on the right pane set the files owner to your view controller:
FOOTNOTE
As an aside, you don't need to set the frame of your view controller in viewDidLoad. It isn't a ViewControllers responsibility to evaluate its own size. A view controller is merely responsible for maintaining the relationship between the model and the view. It is the responsibility of the container or parent ViewController to set the size.
Happy coding

UINavigationBar Back Button show "Back" title on some devices or simulators and the previous view controller title on others

I have an application with a navigation bar that navigates from one view controller to the next.
When navigating to next view controller on some simulators and devices the back button title is "Back" and on some simulator and devices the back button title is the title of the first view controller.
I would really like to understand why?
This is not an issue of changing the text on the back button. The issue is that on some simulators and devices I see the "back" title and on some simulators and devices I see the title of the previous controller.
Tested on over 20 simulators and devices.
Here is the code:
App Delegate
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface AppDelegate ()
#property (nonatomic, strong) UINavigationController *navigationController;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstViewController = [[FirstViewController alloc]
initWithNibName:nil
bundle:nil];
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.rootViewController = self.navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
First View Controller
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController ()
#property (nonatomic, strong) UIButton *btnDisplaySecondViewController;
#end
#implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)performDisplaySecondViewController:(id)paramSender{
SecondViewController *secondControllrt = [[SecondViewController alloc]
initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:secondControllrt
animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"First Controller";
self.btnDisplaySecondViewController = [UIButton
buttonWithType:UIButtonTypeSystem];
[self.btnDisplaySecondViewController
setTitle:#"Display Seconf View Controller"
forState:UIControlStateNormal];
[self.btnDisplaySecondViewController sizeToFit];
self.btnDisplaySecondViewController.center = self.view.center;
[self.btnDisplaySecondViewController addTarget:self
action:#selector(performDisplaySecondViewController:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnDisplaySecondViewController];
UIImageView *imageView =
[[UIImageView alloc]
initWithFrame:CGRectMake(0.0, 0.0, 100.0f, 40.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed:#"ios7_0135"];
[imageView setImage:image];
self.navigationItem.titleView = imageView;
}
Second View Controller
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Second Controller";
}
When you set the title of the current view controller it will be set automatically in place of Back in the navigation bar when you move to the next view controller. And if you didn't set the title of the current view controller then iOS automatically shows the Back title.
To set the title of the view controller, use the following code:
self.title = #"<title>";
I think you will get this from the explanation.
This is an old question, but I found the same just a second ago. For me, it is a matter of display space. On my iPhone 5, in portrait, I get the short "Back" text, if I rotate the view, it will show the previous' controllers title.
There is a different behavior in iOS6 and iOS7. In iOS7 the title of your first controller will be changed to "Back" (on the left navigationItem in the second VC) if its length exceed 11 characters. In iOS6 the title of the navBar gets truncated and/or the title of the back item.
Hi you can check this one Please upward if your problem has been solve so that the other can be benifited from it.
When my app returns from gallery, it has the navbar's standard colors
Additionally, if you want to change the name of your back button to be different than that of the previous view controller's title, you can do so by creating a custom back button item on the navigation item of the controller being pushed onto the navigation controller stack:
custom UIBarButtonItem for back button
Apple's reference here for backBarButtonItem

Gap when changed of viewcontroller

I'm implementing an application, in which i use several view controllers. But i've found a strange behaviour of my app' when it changes of viewcontroller. Let me explain :
We are in a first viewcontroller, his init method is :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
Menu = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
self.view.backgroundColor = [UIColor greenColor];
[self.view addSubview:Menu];
[Menu release];
}
return self;
}
After that, to change of viewcontroller and pass from the first to another, i call this method :
-(void)GoPlay
{
if (!parametresviewcontroller)
{
parametresviewcontroller = [[ParametresViewController alloc]init];
}
UIView *menuView = menuviewcontroller.view;
UIView *paramView = parametresviewcontroller.view;
[menuviewcontroller viewWillAppear:NO];
[parametresviewcontroller viewWillDisappear:NO];
[menuView removeFromSuperview];
[self.view addSubview:paramView];
[menuviewcontroller viewDidAppear:NO];
[parametresviewcontroller viewDidDisappear:NO];
}
So the init method of the second viewcontroller is called, i put it next :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
Parametres = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
Parametres.backgroundColor = [UIColor redColor];
[self.view addSubview:Parametres];
[Parametres release];
}
return self;
}
And the result is :
Why this strange behavior occured ? I really don't understand why the is a gap between the first and the second one...
Thank you ! =)
Ok, The problem is, both the class Is a UIViewController and you have set statusbar for both ViewController's View in XIB file. When you set StatusBar Than the view frame changes its y to 20 pixel below means you have set it to 0 but originally it is 20.
So when you add subView second ViewController's view on the first ViewController's View before doing this set:
secondViewController.view.frame = firstViewController.view.bounds;
. This will solve your problem.
First of all, DO NOT CALL viewWillAppear, viewWillDisappear, viewDidAppear and viewDidDisappear by yourself, this methods are called by the OS in some specific points of a UIViewController life-cycle.
Second, initWithNibName is not the proper method for adding subviews to the controller's view, for that you should use viewWillAppear, viewDidAppear or viewDidLoad.
Third, what you are doing is not changing of view controllers is simply adding a view controller's view to another view controller view, for changing UIViewController you should use addChildViewController: and removeFromParentViewController methods or use UINavigationController
I've found a solution : iPhone hiding StatusBar shifts view down by 20 pix
Use
[self setWantsFullScreenLayout:YES];
in the init method in my case.
Thank you all !

ipad UIPopoverController with UINavigationController

How can I create a UIPopoverController with integrated UINavigationController so I will be able to slide views inside the UIPopoverController left-right (with navigation bar).
UPDATE:
I open popup like this
- (void)showSettingsViewAtSenderForIPad:(id)sender
{
if (!settingsPopoverController_)
{
SettingsPopoverController *settings = [[SettingsPopoverController alloc] init];
settings.valuesGeneratorOptions = valuesGeneratorOptions_; // setting variables
self.settingsPopoverController_ = [[[UIPopoverController alloc] initWithContentViewController:settings] autorelease];
[settingsPopoverController_ setDelegate:self];
[settingsPopoverController_ setPopoverContentSize:CGSizeMake(320, 480)];
[settings release];
}
if (!infoPopoverController_.popoverVisible)
{
[settingsPopoverController_ presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
}
I created a controller which has a NSTableViewController as a root controller in UINavigationController
#interface SettingsPopoverController : UIViewController
{
ValuesGeneratorOptions *valuesGeneratorOptions;
IBOutlet SettingsViewController *settingsViewController;
IBOutlet UINavigationController *navigationController;
}
...
#implementation SettingsPopoverController
...
- (void)viewDidLoad
{
self.settingsViewController.valuesGeneratorOptions = self.valuesGeneratorOptions;
[self.view addSubview:self.navigationController.view];
[super viewDidLoad];
}
...
end
The problem is, that the table is not scrollable inside the popup. It also ignores the table style (initWithStyle not called).
Fix?
SOLUTION:
Found the solution: popOver table view
You create a new nib and a UIViewController. This nib has, as it's top level view, a plain jane UIView and a UINavigationController. The UINavigationController's top UIViewController is whatever view controller you want to display first.
You then display this nib inside your popover controller. In the view did load, you do something like this:
-(void)viewDidLoad
{
[self.view addSubview:self.navigationController.view];
}
This adds your navigation controller's view to your view in your nib, which allows it to be displayed.

Resources