I made a popover class that I could call easily for basic popovers - you give it data and set the size and it should do the rest.
This was working fine until iOS5, now the popover opens but with just the border, no white space or content at all.
I've searched and searched, any ideas you could throw my way would be great.
#protocol BasicPickerDelegate
- (void)basicPickerItemSelected:(NSMutableDictionary *)thisDic;
#end
#interface BasicPickerController : UITableViewController {
// data
IBOutlet NSMutableArray *theData;
// stuff to set
IBOutlet int myWidth;
IBOutlet int myHeight;
IBOutlet int myPopoverNum;
// delegate
id<BasicPickerDelegate> delegate;
}
#property (nonatomic, retain) IBOutlet NSMutableArray *theData;
#property (nonatomic, assign) IBOutlet int myWidth;
#property (nonatomic, assign) IBOutlet int myHeight;
#property (nonatomic, assign) IBOutlet int myPopoverNum;
#property (nonatomic, assign) id<BasicPickerDelegate> delegate;
- (void)setSize;
- (void)checkData;
#end
Then the setSize function is viewDidLoad
- (void)setSize {
if (!myWidth || !myHeight) {
NSLog(#"WIDTH AND HEIGHT NOT SET, SETTING DEFAULTS");
myWidth = 100;
myHeight = 100;
}
self.contentSizeForViewInPopover = CGSizeMake(myWidth, myHeight);
}
Then I call it like this:
- (IBAction)showBasicPopover:(id)sender {
// Show Basic Popover - can be reused
if (basicPicker != nil) {
self.basicPicker = nil;
[self.basicPicker release];
}
self.basicPicker = [[[BasicPickerController alloc] initWithStyle:UITableViewStylePlain] autorelease];
basicPicker.delegate = self;
self.basicPickerPopover = [[[UIPopoverController alloc]
initWithContentViewController:basicPicker] autorelease];
// Give popover the data it needs
NSMutableDictionary *sizeDic = [self returnPopoverSize:[sender tag]];
self.basicPicker.myHeight = [[sizeDic objectForKey:#"height"] intValue];
self.basicPicker.myWidth = [[sizeDic objectForKey:#"width"] intValue];
self.basicPicker.myPopoverNum = [sender tag];
[basicPicker viewDidLoad];
self.basicPicker.theData = [self returnPopoverData:[sender tag]];
NSLog(#"giving popover dic (%d) with %d derps", [sender tag], [self.basicPicker.theData count]);
// Set display settings and show popover
[basicPicker viewWillAppear:YES];
CGRect popoverRect = [self.view convertRect:[sender frame]
fromView:[sender superview]];
[self.basicPickerPopover presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
When I run it, the WIDTH AND HEIGHT NOT SET, SETTIN DEFAULTS dialogue comes up. For some reason it's not reading in the values it's given. Though with some fiddling, even if I can get it to read them in it don't think they are valid and overrides them.
edit: So basically:
With setSize being called in viewDidLoad it doesn't know what the width and height is. So it sets the default.
If setSize isn't called in viewDidLoad, it comes up with "no size" - ie it has popover border but no content in it at all.
When setSize is called in viewWillAppear, viewDidAppear or anything like that (after viewDidLoad) it doesn't actually set the size of the popover.
Finally figured this out.
I needed to assign the width/height variables after creating the popover but before initialising it.
Revised code below:
self.basicPicker = [[[BasicPickerController alloc] initWithStyle:UITableViewStylePlain] autorelease];
// Give popover the data it needs
basicPicker.delegate = self;
NSMutableDictionary *sizeDic = [self returnPopoverSize:[sender tag]];
self.basicPicker.myHeight = [[sizeDic objectForKey:#"height"] intValue];
self.basicPicker.myWidth = [[sizeDic objectForKey:#"width"] intValue];
self.basicPicker.myPopoverNum = [sender tag];
// Now init
self.basicPickerPopover = [[[UIPopoverController alloc]
initWithContentViewController:basicPicker] autorelease];
[basicPicker viewDidLoad];
Related
I have an odd issue where when I change the background of a view, I can no longer see the UILabels that are subviews of that view. I cannot dump the headers for the Calculator app, as it is in Swift, and I don't know what the name of the UILabels are, and FLEXing is no help with that. I want to bring the subviews in front of the background view (there are two subviews), but both of my current methods crash the target app. I was trying to see if I could get it working with just one at the very front though.
My code:
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).allSubviews];
if ([labels count] > 0) {
UILabel *mainLabel = labels[0];
[self bringSubviewToFront:mainLabel];
}
((DisplayView*)self).allSubviews = labels;
And I have also tried:
for (UILabel *label in ((DisplayView*)self).allSubviews) {
if ([label isKindOfClass:[UILabel class]]) {
[self bringSubviewToFront:label];
}
I need an alternative method of doing this, a way to bring all of the subviews forward, or someone to tell me what I am doing wrong.
Edit, here's my full code:
#interface DisplayView
#property (nonatomic, copy, readwrite) UIColor *backgroundColor;
#property (nonatomic, assign, readwrite, getter=isHidden) BOOL hidden;
#property (nonatomic, assign, readwrite, getter=isOpaque) BOOL opaque;
#property (nonatomic, assign, readwrite) CGFloat alpha;
#property (nonatomic, copy, readwrite) NSArray *allSubviews;
#property (nonatomic, assign, readwrite) CALayer *layer;
#end
%hook DisplayView
-(void)layoutSubviews {
((DisplayView*)self).opaque = NO;
((DisplayView*)self).backgroundColor = [UIColor blackColor];
((DisplayView*)self).hidden = NO;
((DisplayView*)self).alpha = 1.0;
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).layer.sublayers];
if ([labels count] > 0) {
UILabel *mainLabel = labels[0];
[self bringSubviewToFront:mainLabel];
}
}
%end
%ctor {
%init(DisplayView=objc_getClass("Calculator.DisplayView"));
}
Seems like a few possible issues. First, there is no allSubviews on UIView, it probably should be subviews.
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).subviews];
if ([labels count] > 0) {
UILabel *mainLabel = labels[0];
(Just want point out that you don't seem to change the array, so why copy it? You can directly access self.subviews)
Second, in the CALayer version, you're casting CALayer to UIView:
NSMutableArray *labels = [NSMutableArray arrayWithArray:((DisplayView*)self).layer.sublayers];
CALayer *mainLabel = labels.firstObject;
if (mainLabel) {
[labels removeObjectAtIndex:0];
[labels addObject:mainLabel];
self.layer.sublayers = labels;
}
Maybe some of this helps
I have a subclassed UIView called BannerHeaderView, and designed the way I want it layed out in an xib. I have set the xib class to BannerHeaderView. I have a UILabel and a UIImageView which I have connected in IB.
The problem is that when I go to set the values of these subViews after initialising my BannerHeaderView in a ViewController, the label and image view are null. I have tried setting them in init also.
Code for BannerHeaderView.h:
#interface BannerHeaderView : UIView
#property (strong, nonatomic) IBOutlet UIImageView *bannerImage;
#property (strong, nonatomic) IBOutlet UILabel *headerLabel;
-(void)setTitle:(NSString *)title;
-(void)setImageFile:(PFFile *)imageFile;
#end
Code for BannerHeaderView.m:
#implementation BannerHeaderView
#synthesize headerLabel, bannerImage;
-(id)init{
self = [super init];
if (self) {
[self customInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self customInit];
}
return self;
}
-(void)customInit{
UIView *contentView = [[[NSBundle mainBundle] loadNibNamed:#"BannerHeaderView"
owner:self
options:nil] objectAtIndex:0];
[self addSubview:contentView];
contentView.frame = self.bounds;
}
-(void)setTitle:(NSString *)title{
NSLog(#"HEADER = %#", headerLabel);
headerLabel.text = title;
}
And I am creating the BannerHeaderImage in my ViewController like so:
bannerHeader = [[BannerHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, bannerHeight + segmentedHeaderHeight)];
[bannerHeader setTitle:#"I AM THE NEW TITLE"];
The log tells me that the headerLabel is null.
Questions:
How do I set the values of these subviews?
Should I just design the layout of this programatically?
Thanks for your time.
P.S. I have spent hours researching this and I can't find a solution. I did find similar questions on SO but none with suitable answers to enable me to solve my problem...
Please set the class BannerHeaderView to File's Owner like this
I'm noob in Obj-c programming. I'm making an iPad application composed of a ViewController that shows me 2 text boxes , a button in the upper side , just to make a sum.
In the same Viewcontroller, i have a TableView and a Button. This Tableview initially shows me an element, but i'd want to refresh this list just clicking the button.
The function linked at the button to refresh the list ( touch down event ) is called " listaPDF " .
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface VPViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITextField *txtPrimoAddendo ;
IBOutlet UITextField *txtSecondoAddendo ;
IBOutlet UILabel *lblTotale ;
IBOutlet UITableView *tabella;
NSMutableArray *lista;
}
#property (nonatomic, retain) IBOutlet UITextField *txtPrimoAddendo;
#property (nonatomic, retain) IBOutlet UITextField *txtSecondoAddendo;
#property (nonatomic, retain) IBOutlet UILabel *lblTotale;
#property (nonatomic, retain) IBOutlet UITableView *tabella;
#property (nonatomic, retain) NSMutableArray *lista;
-(IBAction)somma ;
-(IBAction)listaPDF;
#end
Viewcontroller.m
#import "VPViewController.h"
#interface VPViewController ()
#end
#implementation VPViewController
-(void)somma {
int x = [[txtPrimoAddendo text] intValue];
int y = [[txtSecondoAddendo text] intValue];
int somma = x + y ;
NSString *totale = [NSString stringWithFormat:#"La somma fa : %d", somma];
[lblTotale setText:totale];
[lblTotale setHidden:FALSE];
}
- (void)listaPDF {
int contatore = 1;
NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
for (int i = 0; i < dirFiles.count ; i++) {
NSString *files = dirFiles[i];
int indice = files.length - 4 ;
NSString *extension = [files substringFromIndex:indice];
if([extension isEqual: #".pdf"]) {
NSLog(#"********* File PDF : %#", files);
[lista insertObject:files atIndex:contatore];
contatore++;
}
}
[tabella reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [lista count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CellID";
UITableViewCell *cell = [tabella dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"pdf.png"];
cell.textLabel.text = [lista objectAtIndex:indexPath.row];
return cell;
}
- (void)viewDidLoad
{
tabella.delegate = self;
tabella.dataSource = self;
tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];
[lista insertObject:#"FIRST ELEMENT" atIndex:0];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#synthesize lblTotale;
#synthesize txtPrimoAddendo;
#synthesize txtSecondoAddendo;
#synthesize tabella;
#synthesize lista;
#end
Thanks
I have tried your code as it is.
Why you initializing UITableView object in viewDidLoad. It doesn't need to be allocated if you are adding on Xib file. If you creating UITableView Programmatically then its required. Just comment/remove that initializing line and tableView's delegate methods will be called.
And In 'ListaPDF' function no items is being added to array 'lista', that's why no new entry was visible in UITableView.
NSMutableArray indices begin at 0 - not 1
int contatore = 1;
...
[lista insertObject:files atIndex:contatore];
contatore++;
Use addObject to add an object to a NSMutableArray
[lista addObject:files];
Remove all of your instance variables, because this:
#property (nonatomic, retain) NSMutableArray *lista;
creates an instance variable called _lista - not lista
You have quite a bit of code backwards:
tabella.delegate = self;
tabella.dataSource = self;
tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];
You're trying to set the delegate and datasource before the table is allocated.
It should be:
tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];
tabella.delegate = self;
tabella.dataSource = self;
However the UITableView has been added inside Interface Builder, right?
That means it's already being created for you and you shouldn't be allocating the table here.
Also: inside Interface Builder you can rightclick+drag (or ctrl+leftclick+drag) to connect your UITableView to yourUIViewController as its delegate and datasource
I'm trying to implement an UIScrollView with Page Control with an array of images. I used same piece of code of another project, but it doesn't work with Xcode 4.5. ScrollView frame properties are all 0 while debugging, but the scrollview Outlet is well connected. Images doesn't show in the scroll. It is really strange. I have observed that properties are not synthesized automatically like previous versions. Here is the code:
#interface dashboardViewController : UIViewController <UIScrollViewDelegate>{
BOOL pageControlBeingUsed;
}
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"fondo.jpg"]]];
pageControlBeingUsed=NO;
scrollView.delegate=self;
[self buildScrollView];
// Do any additional setup after loading the view.
}
-(void)buildScrollView{
NSArray *imagesQueue = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"icoblog.png"], [UIImage imageNamed:#"icogaleria.png"], [UIImage imageNamed:#"icobio.png"], nil];
for(int i = 0; i < imagesQueue.count; ++i) {
UIImageView *image = [[UIImageView alloc] initWithImage:[imagesQueue objectAtIndex:i]];
CGFloat xOrigin = i * (scrollView.frame.size.width);
image.frame = CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView addSubview:image];
image=nil;
}
scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imagesQueue.count, self.scrollView.frame.size.height);
}
Many thanks for your help.
Select your scrollview in the storyboard and untick the "auto layout" under file properties
Try pushing "self." infront of all the "scrollView" you don't have it.
I'm currently working on an application which uses a scrollViewController as I need to have a vertical and horizontal scroll so it loads the NIB's nested.
This all worked fine but when I add a UIPicker into one of my nested nibs it does not scroll, I have read this can easily be fixed by disabling scrolling in the UIScrollView however I am not 100% sure how to do this from the other class.
scrollViewController.h
#interface scrollViewController : UIViewController <UIScrollViewDelegate>{
UIScrollView* scrollView;
UIPageControl* pageControl;
UIScrollView *vScrollView;
UIScrollView *hScrollView;
BOOL pageControlBeingUsed;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
#property (nonatomic, retain) UIScrollView *vScrollView;
#property (nonatomic, retain) UIScrollView *hScrollView;
- (IBAction)changePage;
#end
scrollViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect bounds = self.view.bounds;
// main guy is a horizontal scroller
hScrollView = [[UIScrollView alloc] initWithFrame:bounds];
hScrollView.contentSize = CGSizeMake(bounds.size.width * 2, bounds.size.height);
hScrollView.delegate = self;
[self.view addSubview:hScrollView];
// the horizontal scroller contains a vertical scroller
vScrollView = [[UIScrollView alloc] initWithFrame:bounds];
vScrollView.contentSize = CGSizeMake(bounds.size.width, bounds.size.height * 2);
vScrollView.delegate = self;
[hScrollView addSubview:vScrollView];
2ndView *view2 = [[2ndView alloc] initWithNibName:#"2ndView" bundle:nil];
[vScrollView addSubview:view2.view];
vScrollView.contentOffset = CGPointMake(0, bounds.size.height);
mainView *vc = [[mainView alloc] initWithNibName:#"mainView" bundle:nil];
vc.view.frame = CGRectOffset(bounds, 0, bounds.size.height);
[vScrollView addSubview:vc.view];
// 3rd View
ValidatorView *vc3 = [[ValidatorView alloc] initWithNibName:#"ValidatorView" bundle:nil];
vc3.view.frame = CGRectOffset(bounds, bounds.size.width, 0);
[hScrollView addSubview:vc3.view];
// enable paging in both directions
hScrollView.pagingEnabled = TRUE;
vScrollView.pagingEnabled = TRUE;
hScrollView.showsHorizontalScrollIndicator = FALSE;
vScrollView.showsVerticalScrollIndicator = FALSE;
hScrollView.alwaysBounceHorizontal = FALSE;
vScrollView.alwaysBounceVertical = FALSE;
vScrollView.canCancelContentTouches = NO;
vScrollView.delaysContentTouches = NO;
hScrollView.bounces = FALSE;
vScrollView.bounces = FALSE;
}
In mainView.m I have created a button which brings a UIPicker onto the view, however I want it to update the vScrollView in scrollViewController with the property
vScrollView.scrollEnabled = NO;
Any help on this would be appreciated.
Thanks Aaron
You can have an instance variable in your application delegate class that points to your scrollViewController and then you can use scrollEnabled of that instance variable to NO