preferredContentSize using UIModalPresentationPopover when rotate - popover

I want to present a UItableViewController when a button is press using popoverPresentationController.
It works ok if the device is not rotated, but if the device is rotated the preferredContentSize it is not ok.
The code:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
#import "ViewController.h"
#import "TableViewController.h"
#interface ViewController () <UIAdaptivePresentationControllerDelegate>
#end
#implementation ViewController
- (IBAction)showActionButtonPress:(id)sender {
TableViewController *tableViewController = [[self storyboard] instantiateViewControllerWithIdentifier:#"TableViewController"];
tableViewController.modalPresentationStyle = UIModalPresentationPopover;
tableViewController.popoverPresentationController.sourceView = sender;
tableViewController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown;
UIPresentationController *presentationController = tableViewController.presentationController;
presentationController.delegate = self;
tableViewController.preferredContentSize = CGSizeMake(414.0, 174.0);
[self presentViewController:tableViewController animated:YES completion:nil];
}
#pragma mark - <UIAdaptivePresentationControllerDelegate>
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
NSLog(#"%#: %#", NSStringFromSelector(_cmd), self);
return UIModalPresentationNone;
}
#end
#import <UIKit/UIKit.h>
#interface TableViewController : UITableViewController
#end
#import "TableViewController.h"
#import "TableViewCell.h"
#implementation TableViewController
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
// return 210;
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//return UITableViewAutomaticDimension;
return 174.0;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"%#: %#", NSStringFromSelector(_cmd), self);
NSString *CellIdentifier = #"CellIdentifier";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.imageView.image = [UIImage imageNamed:#"ball.png"];
cell.label1.text = #"One";
cell.label2.text = #"Two";
cell.label3.text = #"Three";
cell.label4.text = #"Four";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#end
#import <UIKit/UIKit.h>
#interface TableViewCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UIImageView *cellImageView;
#property (nonatomic, strong) IBOutlet UILabel *label1;
#property (nonatomic, strong) IBOutlet UILabel *label2;
#property (nonatomic, strong) IBOutlet UILabel *label3;
#property (nonatomic, strong) IBOutlet UILabel *label4;
#end
#import "TableViewCell.h"
#implementation TableViewCell
#end
Image Portrait:
Image LandScape:

Related

Compilation fails due to prototype cell

I have a TableViewController subclass with one prototype cell designed in storyboard. Then I am passing the references to the class header etc and for some reason it fails to compile it. But the connections seem fine. I provide you the code and pictures of the build errors and the storyboard. I am using Xcode 4.3.3. Any help is really appreciated.
favTable.h
#import <UIKit/UIKit.h>
#interface favTable : UITableViewController <NSFetchedResultsControllerDelegate>
{
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
NSArray *favArr;
NSMutableArray *favName;
NSMutableArray *favScore;
}
#property (nonatomic, retain) NSArray *favArr;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) NSMutableArray *favName;
#property (nonatomic, strong) NSMutableArray *favScore;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (strong, nonatomic) IBOutlet UITableViewCell *celldes;
#property (strong, nonatomic) IBOutlet UIImageView *cellimage;
#property (strong, nonatomic) IBOutlet UILabel *cellname;
#property (strong, nonatomic) IBOutlet UILabel *cellmanu;
#property (strong, nonatomic) IBOutlet UILabel *cellscore;
#end
favTable.m
#import "favTable.h"
#import "ecoAppDelegate.h"
#interface favTable ()
#end
#implementation favTable
#synthesize favArr;
#synthesize managedObjectContext;
#synthesize fetchedResultsController;
#synthesize favName;
#synthesize favScore;
#synthesize celldes;
#synthesize cellimage;
#synthesize cellname;
#synthesize cellmanu;
#synthesize cellscore;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Favorites";
self.navigationController.navigationBar.translucent = NO;
// passing the array of addedtofavorites to the total one with all favorites
self.managedObjectContext = ((ecoAppDelegate *) [UIApplication sharedApplication].delegate).managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"FavoritesInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.resultType = NSDictionaryResultType;
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:#"name", nil]];
NSError *error=nil;
self.favArr=[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]mutableCopy];
if (error!=nil) {
NSLog(#" fetchError=%#,details=%#",error,error.userInfo);
}
self.favName = [[self.favArr valueForKey:#"name"] mutableCopy];
self.favScore = [[self.favArr valueForKey:#"score"] mutableCopy];
}
- (void)viewDidUnload
{
[self setCelldes:nil];
[self setCellimage:nil];
[self setCellname:nil];
[self setCellmanu:nil];
[self setCellscore:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [favName count];;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
// Configure the cell...
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cellname.text = #"Test";
return cell;
}
#end
I moved manually the IB outlets into a separate custom cell class and then linked the references to the storyboard (maybe this is fixed in newer versions)
Then applied that cell style in the prototype cell and simply changed the content of the UI items of my cell using the identifier (no tag was needed)

UICollectionView - Please help- getting a "type NSException" error when running

I'm totally new to Xcode. This is my first attempt at UICollectionView. Any help is greatly appreciated.
This is the error I am getting.
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
here is my code.
PatternViewCell.h
#import <UIKit/UIKit.h>
#interface PatternViewCell : UICollectionViewCell
#property ( nonatomic, weak)IBOutlet UIImageView *patternImageView;
#property ( nonatomic, weak)IBOutlet UILabel *patternLabel;
#end
#import <UIKit/UIKit.h>
#import "PatternViewCell.h"
#interface collectionViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
#property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
#end
#import "collectionViewController.h"
#interface collectionViewController ()
#property ( nonatomic, strong) NSArray * patternImagesArray;
#end
#implementation collectionViewController
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PatternViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"PatternCell" forIndexPath:indexPath];
NSString * myPatternString = [self.patternImagesArray objectAtIndex:indexPath.row];
cell.patternImageView.image = [UIImage imageNamed:myPatternString];
cell.patternLabel.text = myPatternString;
return cell;
}
-(CGSize) collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(150, 150);
}
-(UIEdgeInsets) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.patternImagesArray = #[#"1.png",#"2.png"#"3.png"#"4.png"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.patternImagesArray.count;
}
#end
_
Hi make sure to register your cell xib file.
Pop this:
[self.collectionView registerNib:[UINib nibWithNibName:#"YourXibCellNameHere" bundle:nil] forCellWithReuseIdentifier:#"PatternCell"];
into your "ViewDidLoad" method in the collectionViewController class. The application needs to know the xib file to populate the UIContentView with.
If you are not using Xib file for your cell just use the name of your custom cell view controller.
Good luck!

Problems displaying plist in UITableView using storyboards? (Xcode)

I have done this on another project before and got it working then, but I cannot find out why this piece of my programming will not work.
It displays the table without any of the plist data on it.
Please take a look through my code to see if you can spot where I have made a mistake, thanks.
#import "MarketViewController.h"
#import "SecondViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#interface MarketViewController ()
#end
#implementation MarketViewController
#synthesize shares, shareValues, number, shareName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
self.navigationItem.title = #"Stock Market";
NSString *sharesFile = [[NSBundle mainBundle] pathForResource:#"Shares" ofType:#"plist"];
shares = [[NSDictionary alloc] initWithContentsOfFile: sharesFile];
shareValues = [shares objectForKey:#"Share Values"];
shareName = [shares objectForKey:#"Share Names"];
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_menuBtn.frame = CGRectMake(8, 10, 34, 24);
[_menuBtn setBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal];
[_menuBtn addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.menuBtn];
}
//Table View
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [shares count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = #"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSString *nameOfShare = [shareName objectAtIndex:[indexPath row]];
NSString *valueOfShare = [shareValues objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = valueOfShare;
cell.textLabel.text = nameOfShare;
return cell;
self.title = #"Stock Market";
}
- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
My h. file is as follows:
#import <UIKit/UIKit.h>
#interface MarketViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) UIButton *menuBtn;
#property (nonatomic, readonly) NSDate *CurrentDate;
#property (nonatomic, strong) NSDictionary *shares;
#property (nonatomic, strong) NSArray *shareValue;
#property (nonatomic, strong) NSArray *number;
#property (nonatomic, strong) NSArray *shareName;
- (NSString*)filePath;
- (void)openDB;
#end

Save UIImageView When Unload A View - Xcode 4.5.1

Creating an app on Xcode 4.5.1 and wondering how do i save the current image in the specified UIImageView when i navigate to a different view. Then when i return to the View it loads up the saved image into the UIImageView?
Thank a lot, appreciate it :D
The code I've posted below involves passing a uiimage to a second view controller. It also includes a button on each controller to move back and forth between the two. The saved image will stay in the uiimageview when you switch to and from the views.
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UIImageView *yourphotoview;
UIImage *yourimage;
}
#property (retain, nonatomic) UIImage *yourimage;
- (IBAction)back;
#end
SecondView.m
#import "ViewController.h"
#import "SecondView.h"
#interface SecondView ()
#end
#implementation SecondView
#synthesize yourimage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
yourphotoview.image = image;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)dealloc {
[image release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
#interface ViewController : UIViewController
{
IBOutlet UISlider *compression;
IBOutlet UISwitch *smurferderp;
SecondView *SecondViewdata;
}
#property (retain, nonatomic) SecondView *SecondViewdata;
#property (retain, nonatomic) IBOutlet UIImageView *theimage;
- (IBAction)switchview:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize SecondViewdata, theimage;
- (IBAction)switchview:(id)sender {
SecondView *secondview = [self.storyboard instantiateViewControllerWithIdentifier:#"rr"];
self.SecondViewdata = secondview;
SecondViewdata.yourimage = theimage.image;
[self presentViewController: secondview animated:YES completion:NULL];
}
- (void)dealloc {
[theimage release];
[super dealloc];
}
#end

NSTableView: no text in new rows

I wrote a method to add new rows to NSTableView. It adds new rows, but there're empty, without text.
Here's code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSButton *addDreamButton;
IBOutlet NSButton *removeDreamButton;
IBOutlet NSTextField *dreamField;
IBOutlet NSTableView *dreamTable;
IBOutlet NSMutableArray *dreamlist;
IBOutlet NSTextField *testf;
}
- (IBAction)addDream:(id)sender;
- (IBAction)deleteDream:(id)sender;
#property (assign) IBOutlet NSWindow *window;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window;
-(int) numberOfRowsInTableView: (NSTableView *) tableView
{
return [dreamlist count];
}
-(void) dreamTable: (NSTableView *) tableview
setObjectValue:(id)anObject
objectValueForTableColumn: (NSTableColumn *) tablecolumn
row:(int) rowIndex
{
[dreamlist replaceObjectAtIndex:rowIndex withObject:anObject];
}
- (IBAction)addDream:(id)sender
{
NSString* thedream = dreamField.stringValue;
[dreamlist addObject: thedream];
[dreamTable reloadData];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
dreamlist = [[NSMutableArray alloc] init];
}
#end
AppDelegate is bound as datasource with TableView.
So when I press "add" button empty rows appears in table (textfield where I get text is not empty; I debugged and saw that MutableArray contains right strings).
Your table view data source needs to implement this method:
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [dreamlist objectAtIndex:rowIndex];
}

Resources