I have gone through a few tutorials including the sample app included
with Three20 and cannot figure out why photos aren't showing up in my
TTPhotoViewController. I actually find it pretty hard to debug.
Below is the code I have. Any thoughts on why images will not load
and how to debug it would be great. I get a completely black view in
between my bottom tabbar and upper nav bar. I also see left and right
arrows overlayed on the black view which seems to be for navigating
photos although I thought it was supposed to display a thumbnail
gallery.
// A TTPhoto class
// Photo.h
#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#interface Photo : NSObject <TTPhoto> {
NSString *_caption;
NSString *_urlLarge;
NSString *_urlSmall;
NSString *_urlThumb;
id <TTPhotoSource> _photoSource;
CGSize _size;
NSInteger _index;
}
#property (nonatomic, copy) NSString *caption;
#property (nonatomic, copy) NSString *urlLarge;
#property (nonatomic, copy) NSString *urlSmall;
#property (nonatomic, copy) NSString *urlThumb;
#property (nonatomic, assign) id <TTPhotoSource> photoSource;
#property (nonatomic) CGSize size;
#property (nonatomic) NSInteger index;
- (id)initWithCaption:(NSString *)caption urlLarge:(NSString
*)urlLarge urlSmall:(NSString *)urlSmall urlThumb:(NSString *)urlThumb
size:(CGSize)size;
#end
// Photo.m
#import "Photo.h"
#implementation Photo
#synthesize caption = _caption;
#synthesize urlLarge = _urlLarge;
#synthesize urlSmall = _urlSmall;
#synthesize urlThumb = _urlThumb;
#synthesize photoSource = _photoSource;
#synthesize size = _size;
#synthesize index = _index;
- (id)initWithCaption:(NSString *)caption urlLarge:(NSString
*)urlLarge urlSmall:(NSString *)urlSmall urlThumb:(NSString *)urlThumb
size:(CGSize)size {
if ((self = [super init])) {
self.caption = caption;
self.urlLarge = urlLarge;
self.urlSmall = urlSmall;
self.urlThumb = urlThumb;
self.size = size;
self.index = NSIntegerMax;
self.photoSource = nil;
}
return self;
}
- (void) dealloc {
self.caption = nil;
self.urlLarge = nil;
self.urlSmall = nil;
self.urlThumb = nil;
[super dealloc];
}
#pragma mark TTPhoto
- (NSString*)URLForVersion:(TTPhotoVersion)version {
switch (version) {
case TTPhotoVersionLarge:
return _urlLarge;
case TTPhotoVersionMedium:
return _urlLarge;
case TTPhotoVersionSmall:
return _urlSmall;
case TTPhotoVersionThumbnail:
return _urlThumb;
default:
return nil;
}
}
#end
// A TTPhotoSource class
// PhotoSet.h
#import <Foundation/Foundation.h>
#import "Three20/Three20.h"
#interface PhotoSet : TTURLRequestModel <TTPhotoSource> {
NSString *_title;
NSArray *_photos;
NSArray* _tempPhotos;
NSTimer* _fakeLoadTimer;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, retain) NSArray *photos;
- (id) init;
#end
// PhotoSet.m
#import "PhotoSet.h"
#import "Photo.h"
#implementation PhotoSet
#synthesize title = _title;
#synthesize photos = _photos;
- (id) init {
_title = #"Test photo album";
_photos = [[NSArray alloc] initWithObjects:
[[[Photo alloc] initWithCaption:#"coming soon"
urlLarge:#"http://farm5.static.flickr.com/
4066/4653156849_0905e6b58e_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4066/4653156849_0d15f0e3f0_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4066/4653156849_0d15f0e3f0_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
[[[Photo alloc] initWithCaption:#"coming soon 2"
urlLarge:#"http://farm5.static.flickr.com/
4023/4653774402_05e6acd995_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
[[[Photo alloc] initWithCaption:#"coming soon 2"
urlLarge:#"http://farm5.static.flickr.com/
4023/4653774402_05e6acd995_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
[[[Photo alloc] initWithCaption:#"coming soon 2"
urlLarge:#"http://farm5.static.flickr.com/
4023/4653774402_05e6acd995_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
nil];
for (int i = 0; i < _photos.count; ++i) {
id<TTPhoto> photo = [_photos objectAtIndex:i];
if ((NSNull*)photo != [NSNull null]) {
NSLog(#"in here 65434");
photo.photoSource = self;
photo.index = i;
}
}
return self;
}
- (void) dealloc {
self.title = nil;
self.photos = nil;
[super dealloc];
}
#pragma mark TTModel
- (BOOL)isLoading {
return NO;
}
- (BOOL)isLoaded {
return YES;
}
#pragma mark TTPhotoSource
- (NSInteger)numberOfPhotos {
return _photos.count;
}
- (NSInteger)maxPhotoIndex {
return _photos.count-1;
}
- (id<TTPhoto>)photoAtIndex:(NSInteger)photoIndex {
if (photoIndex < _photos.count) {
return [_photos objectAtIndex:photoIndex];
} else {
return nil;
}
}
#end
// A TTPhotoViewController
// EventDetailViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#import "PhotoSet.h"
#class Event;
#interface EventDetailViewController :
TTPhotoViewController<UINavigationControllerDelegate,
UIImagePickerControllerDelegate> {
NSArray *photos;
Event *event;
PhotoSet *_photoSet;
}
#property (nonatomic, retain) NSArray *photos;
#property (nonatomic, retain) Event *event;
#property (nonatomic, retain) PhotoSet *photoSet;
- (id)initWithEvent:(Event *)e;
// EventDetailViewController.m
#import "EventDetailViewController.h"
#import "Event.h"
#import "PhotoSet.h"
#import "Photo.h"
#implementation EventDetailViewController
#synthesize photos;
#synthesize event;
#synthesize photoSet = _photoSet;
#pragma mark -
#pragma mark Initialization
- (void)viewDidLoad {
self.photoSet = [[PhotoSet alloc] init];
self.photoSource = self.photoSet;
}
- (id)initWithEvent:(Event *)e {
if (self) {
self.event = e;
}
return self;
}
#end
You may find an answer here: http://www.raywenderlich.com/1430/how-to-use-the-three20-photo-viewer
Related
[__NSArrayM insertObject:atIndex:]: object cannot be nil
The problem is when trying to execute [_eventsArray addObject:eventModel]; error is occurred.
eventModel is nil.
How can I solve this problem ?
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray *eventsArray;
#end
#interface EventsModel : NSObject
#property(nonatomic, assign) NSString *title;
#property(nonatomic, strong) NSString *details;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
EventsModel *eventModel ;
eventModel.title = #"Meeting";
eventModel.details = #"some description";
[_eventsArray addObject:eventModel];
}
#end
#pragma mark - Model Class
#implementation EventsModel
-(instancetype)init{
self = [super init];
if (self) {
self.title = nil;
self.details =nil;
}
return self;
}
#end
EventsModel *model = [EventsModel new];
model.title = #"test";
model.details = #"Detail test";
EventsModel *model2 = [EventsModel new];
model2.title = #"test2";
model2.details = #"Detail test2";
[_eventsArray addObject:model];
[_eventsArray addObject:model2];
NSLog(#"%lu elements in array",(unsigned long)_eventsArray.count);
you use EventsModel *model = [EvenntsModel new]; and create object
try this code
I'm trying to implement KVOController in my app. I managed to get it working on custom UISliders, but I can't seem to get it working on a custom NSObject. This is the object:
Restaurant.h
#import <UIKit/UIKit.h>
#interface Restaurant : NSObject
#property (nonatomic, copy, readonly) NSString *name, *tagline;
#property (nonatomic, copy, readonly) UIColor *uicolor;
- (id)initWithName:(NSString *)main;
- (void)setName:(NSString *)title;
- (void)changeColor:(UIColor)color;
#end
Restaurant.m
#implementation Restaurant
- (id)initWithName:(NSString *)main {
self = [super init];
if (self)
{
_name = main;
}
return self;
}
- (void)setName:(NSString *)title {
_name = title;
}
- (void)changeColor:(UIColor)color {
_uicolor = color;
}
#end
So, I introduced this listener into my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.restaurant = [[Restaurant alloc] initWithName:#"test"];
[self.restaurant changeColor:[UIColor blueColor]];
FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];
self.KVOController = KVOController;
[self.KVOController observe:self.restaurant keyPath:#"name" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
NSLog(#"Restaurant changed!");
}];
}
- (void)someOtherMethod:(id)sender {
[self.restaurant setName:#"Another test"];
[self.restaurant changeColor:[UIColor redColor]];
}
However, this isn't logging anything for this, nor when I change the keyPath to #"uicolor". What am I doing wrong?
Please help....I'm losing my mind trying to figure out this problem.
I'm fairly new to iOS so don't go too hard on me if it's something obvious! ;)
I'm using xcode 4.6 and targeting iPhone6.1 Simulator.
I get the following error when starting up my app:
EXC_BAD_ACCESS code = 2
There are hundres of threads appearing in Debug Navigator which leads to to believe there is some sort of infinite loop somewhere (I just cannot see where).
The error occurs beside (id)init in PlayingCardDeck.m after entering it from ViewController.m at line:
Card *card = [self.deck drawRandonCard];
ViewConrtoller:
#import "ViewController.h"
#import "PlayingCardDeck.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
#property (nonatomic) int flipCount;
#property (strong, nonatomic) Deck *deck;
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
#end
#implementation ViewController
#synthesize deck = _deck;
- (IBAction)flipCard:(UIButton *)sender {
sender.selected = !sender.isSelected;
self.flipCount++;
}
- (void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:#"Flips: %d", self.flipCount];
}
- (Deck *)deck
{
if (!_deck) _deck = [[PlayingCardDeck alloc] init];
return _deck;
}
- (void)setCardButtons:(NSArray *)cardButtons
{
_cardButtons = cardButtons;
for (UIButton *cardButton in cardButtons)
{
Card *card = [self.deck drawRandonCard];
[cardButton setTitle:card.contents forState:UIControlStateSelected];
}
}
#end
Deck.m
#import "Deck.h"
#interface Deck()
#property (strong, nonatomic) NSMutableArray *cards;
#end
#implementation Deck
- (NSMutableArray *)cards
{
if (!_cards) _cards = [[NSMutableArray alloc] init];
return _cards;
}
- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
if (atTop)
{
[self.cards insertObject:card atIndex:0];
}
else
{
[self.cards addObject:card];
}
}
- (Card *)drawRandonCard
{
Card *randomCard = nil;
if (self.cards.count)
{
unsigned index = arc4random() % self.cards.count;
randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
}
return randomCard;
}
#end
PlayingCardDeck.m
#import "PlayingCardDeck.h"
#import "PlayingCard.h"
#implementation PlayingCardDeck
- (id)init
{
self = [self init];
if (self)
{
for (NSString *suit in [PlayingCard validSuits])
{
for (NSUInteger rank=1; rank <= [PlayingCard maxRank]; rank++)
{
PlayingCard *card = [[PlayingCard alloc] init];
card.suit = suit;
card.rank = rank;
[self addCard:card atTop:YES];
}
}
}
return self;
}
#end
In PlayerCardDeck.m self = [self init] should be self = [super init]. That's causing the infinite loop.
I'm making a Card game and trying to call UIImages from an object's instance variable to update a UIImageView
I have a Deck object, which has an NSArray instance variable of Card objects.
Each Card object has a few instance variables, one of which is an UIImage that I'm trying to display in a UIImageView....and this is where I'm having a problem
The storyboard isn't displaying the UIImageView and i'm not getting any compile errors
The UIImageView that I'm trying to update is cardDisplay (ViewController.h)
Here's some snippets from my code
ViewController.h
#import "Deck.h"
#import "Card.h"
#interface ViewController : UIViewController
{
UIImageView *cardDisplay;
}
#property (nonatomic, retain) IBOutlet UIImageView *cardDisplay;
#end
ViewController.m
#import "ViewController.h"
#import "Deck.h"
#import "Card.h"
#implementation ViewController
#synthesize cardDisplay;
- (void)viewDidLoad
{
[super viewDidLoad];
Deck *deck = [[Deck alloc]init];
NSLog(#"%#", deck);
for (id cards in deck.cards) {
NSLog(#"%#", cards);
}
self.cardDisplay = [[UIImageView alloc] initWithImage:
[[deck.cards objectAtIndex:0 ] cardImage]];
}
#end
Card.h
#interface Card : NSObject
{
NSString *valueAsString, *suitAsString;
NSInteger faceValue, countValue;
Suit suit;
UIImage *cardImage;
}
#property (nonatomic, retain) NSString *valueAsString;
#property (nonatomic, retain) NSString *suitAsString;
#property (nonatomic) NSInteger faceValue;
#property (nonatomic) NSInteger countValue;
#property (nonatomic) Suit suit;
#property (nonatomic) UIImage *cardImage;
- (id) initWithFaceValue:(NSInteger)aFaceValue countValue:(NSInteger)aCountValue
suit:(Suit)aSuit cardImage:(UIImage*)aCardImage;
#end
Deck.h
#import "Card.h"
#interface Deck : NSObject
{
NSMutableArray *cards;
}
#property(nonatomic, retain)NSMutableArray *cards;
#end
Deck.m
#import "Deck.h"
#import "Card.h"
#implementation Deck
#synthesize cards;
- (id) init
{
if(self = [super init])
{
cards = [[NSMutableArray alloc] init];
NSInteger aCount, picNum = 0;
for(int suit = 0; suit < 4; suit++)
{
for(int face = 1; face < 14; face++, picNum++)
{
if (face > 1 && face < 7)
aCount = 1;
else if (face > 6 && face < 10)
aCount = 0;
else
aCount = -1;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *imagePath = [path stringByAppendingPathComponent:
[NSString stringWithFormat:#"/cards/card_%d.png",picNum]];
UIImage *output = [UIImage imageNamed:imagePath];
Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
countValue:(NSInteger)aCount
suit:(Suit)suit
cardImage:(UIImage *)output];
[cards addObject:card];
}
}
}
return self;
}
#end
This line:
self.cardDisplay = [[UIImageView alloc] initWithImage:
[[deck.cards objectAtIndex:0 ] cardImage]];
should be:
self.cardDisplay.image = [[deck.cards objectAtIndex:0 ] cardImage];
You need to set the image on the image view you created in IB, not create a new one. Doing it this way doesn't keep you from doing what you want with the timer later.
I have a singleton object in iOS that when instantiated parses a CSV file and then holds the results. I would like to make this object universally accessible and I would like it to not be released from memory until the app quits. I am running ARC so I cannot do manual retains.
Is there a way I can do this so it will work with ARC?
Header File:
#import <Foundation/Foundation.h>
#import "CHCSV.h"
#import "RCParserObject.h"
#interface ParserStore : NSObject <CHCSVParserDelegate>
{
// CSV Variables
RCParserObject *item;
NSMutableArray *data;
NSMutableArray *parsedData;
int fields;
bool open;
}
#property (atomic, retain) RCParserObject *item;
#property (atomic, retain) NSMutableArray *data;
#property (atomic, retain) NSMutableArray *parsedData;
#property (atomic) int fields;
#property (atomic) bool open;
+ (ParserStore *) defaultStore;
- (void) parseCSVFile:(NSString*)file;
- (void) categorizeData;
Implementation File
#import "ParserStore.h"
#import "RCParserObject.h"
#import "RCDetailItem.h"
static ParserStore *defaultStore;
#implementation ParserStore
#synthesize item, data, parsedData, fields, open;
# pragma mark -
# pragma mark Singleton Methods
+ (ParserStore *) defaultStore
{
if (!defaultStore)
{
defaultStore = [[super allocWithZone:NULL] init];
}
return defaultStore;
}
+ (id) allocWithZone:(NSZone *)zone
{
return [self defaultStore];
}
- (id) init
{
NSLog(#"Running init on parser store");
if (defaultStore)
{
NSLog(#"Self data count is %d, right before return", self.parsedData.count);
return defaultStore;
}
// NSLog(#"This better only happen once");
self = [super init];
[self setParsedData:[[NSMutableArray alloc] init]];
[self parseCSVFile:#"ContentNoPathFileExt2ASCII"];
[self categorizeData];
// NSLog(#"Self data count is %d when first created", self.parsedData.count);
return self;
}
#property (atomic, retain) RCParserObject *item;
#property (atomic, retain) NSMutableArray *data;
#property (atomic, retain) NSMutableArray *parsedData;
#property (atomic) int fields;
#property (atomic) bool open;
+ (ParserStore *) defaultStore;
- (void) parseCSVFile:(NSString*)file;
- (void) categorizeData;
#end
+(MySingleton *)singleton {
static dispatch_once_t pred;
static MySingleton *shared = nil;
dispatch_once(&pred, ^{
shared = [[MySingleton alloc] init];
shared.someVar = someValue; // if you want to initialize an ivar
});
return shared;
}
From anywhere:
NSLog(#"%#",[MySingleton singleton].someVar);
Note that your iOS app already has a singleton that you can access anywhere:
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
a simple singleton would be something like... in the .h:
#interface Foo : NSObject
+ (Foo *)sharedInstance;
#end
and in the .m:
static Foo *_foo = nil;
#implementation Foo
+ (Foo *)sharedInstance {
if (!_foo)
_foo = [[Foo alloc] init];
return _foo;
}
#end