MWPhotoBrowser with button and error handeling - iphone

I'm trying to make a app that uses a photobrowser
Decided to go with the MWPhotoBrowser.
This is the code, but i cant seem to make it work:
ViewController.h
#import <UIKit/UIKit.h>
#import "MWPhotoBrowser.h"
#interface ViewController : UIViewController<MWPhotoBrowserDelegate> {
NSArray *_photos;
UISegmentedControl *_segmentedControl;
}
#property (nonatomic, retain) NSArray *photos;
- (IBAction)billede:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize photos = _photos;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:_segmentedControl] autorelease];
self.navigationItem.rightBarButtonItem = item;
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)billede:(id)sender {
//show your photo whit url
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
{
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"photo2l" ofType:#"jpg"]];
photo.caption = #"The London Eye is a giant Ferris wheel situated on the banks of the River Thames, in London, England.";
[photos addObject:photo];
}
self.photos = photos;
// Create browser
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.displayActionButton = YES;
//browser.wantsFullScreenLayout = NO;
//[browser setInitialPageIndex:2];
// Show
if (_segmentedControl.selectedSegmentIndex == 0) {
// Push
[self.navigationController pushViewController:browser animated:YES];
} else {
// Modal
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:nc animated:YES];
[nc release];
}
}
#pragma mark - MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex: (NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
#end
I've tried both with ARC and wothout ARC
Without ARC i get 3 errors:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_MWPhoto", referenced from:
objc-class-ref in ViewController.o
"_OBJC_CLASS_$_MWPhotoBrowser", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
With ARC i get 2 errors
Problem 1
http://i.imgur.com/pT7qW.png
Problem 2
http://i.imgur.com/E0X1m.png
I've not done it here, but i would like it alle to be wrapped in a button so i can click that and show a image inside the MWPhotoBrowser
Edit
I have upgraded my code, removed ARC from my files and i have now set the target right.
It will compile now, but everytime i triy to click the button: "billede" i get:
2012-11-26 23:32:10.955 MWPhotoBrowserTest[10405:c07] -[ViewController galleri:]: unrecognized selector sent to instance 0x947fb20
2012-11-26 23:32:10.957 MWPhotoBrowserTest[10405:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController galleri:]: unrecognized selector sent to instance 0x947fb20'
*** First throw call stack:
(0x1d34012 0x14e9e7e 0x1dbf4bd 0x1d23bbc 0x1d2394e 0x14fd705 0x434920 0x4348b8 0x4f5671 0x4f5bcf 0x4f4d38 0x46433f 0x464552 0x4423aa 0x433cf8 0x1f6ddf9 0x1f6dad0 0x1ca9bf5 0x1ca9962 0x1cdabb6 0x1cd9f44 0x1cd9e1b 0x1f6c7e3 0x1f6c668 0x43165c 0x1e2d 0x1d55)
libc++abi.dylib: terminate called throwing an exception
(lldb)

Attachment 1:
You can't add objects to NSArray, use NSMutableArray instead.
Attachment 2:
UIView has no methods "reloadData", but for exmple UITableView does.

Actually resolved this.
I've struggled for 4 days and finally got it.
I read through https://github.com/mwaterfall/MWPhotoBrowser#method-1-static-library and understood it, finally but i had a problem with User header Search Path.
I finally got it, importet MWPhotoBrowser folder into my MWPhotoBrowser"project" folder and then sat User Header Search Path to ".." instead of "../" since "../" gave me "../../"
Hope this helps anyone who might stand in the same problem as me!
Btw, thanks for the help guys! :-)

Related

' entityForName: nil is not a legal NSManagedObjectContext -CoreData

I am getting this error when running the app:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity
name 'Event''
This is my TimeTableController.h file (TimeTableController is a subclass UITableViewController and app loads this file when app stared.
TimeTableController.h:
#import "Event.h"
#interface TimeTableController : UITableViewController
{
NSManagedObjectContext *managedObjectContext;
NSMutableArray *eventArray;
}
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) NSMutableArray *eventArray;
- (void) fetchRecords;
- (void) addTime:(id)sender;
#end
Event.h and Event.m are my model files.
And TimeTableController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Lap Times";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addTime:)];
self.navigationItem.rightBarButtonItem = addButton;
//[addButton release];
[self fetchRecords];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)fetchRecords {
// This line gives the error
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Event" inManagedObjectContext:managedObjectContext];
}
The line in fetchRecords method is giving the error.
And this is my AppDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
TimeTableController *tableController = [[TimeTableController alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
//[tableController release];
[window addSubview: [self.navigationController view]];
[window makeKeyAndVisible];
return YES;
}
When I've created the project,Data Model file didn't exist so I've added the Data Model file (LapTimer.xcdatamodeld) to existing project.
I tried all solutions I found and I have no idea why I am getting this error now.
Note: I know there are a lot of threads that cover exactly this questions. I looked at all but none of answers didn't work for me.
I was getting the same error message and it turns out that managedObjectContext was nil.

UIAlertView crashes when added to app

I am trying to impliment a basic UIAlertView into my app (I am using storyboard). But when I run my app, it goes to the debugger screen after stalling at the app's loading screen.
my .h:
#import <UIKit/UIKit.h>
#interface MindTripAnim :UIViewController {
IBOutlet UIImageView *animation;
}
- (IBAction)showMessage:(id)sender;
#end
and my .m:
#import "MindTripAnim.h"
#interface MindTripAnim ()
#end
#implementation MindTripAnim
- (void)viewDidLoad {
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"trips1.png"],
[UIImage imageNamed:#"trips2.png"],
[UIImage imageNamed:#"trips3.png"],
[UIImage imageNamed:#"trips4.png"],
[UIImage imageNamed:#"trips5.png"],
[UIImage imageNamed:#"trips6.png"],nil];
[animation setAnimationRepeatCount:0];
animation.animationDuration = 0.65;
[animation startAnimating];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showMessage:(id)sender
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Hello World!" message:#"This is your first UIAlertview message." delegate:nil cancelButtonTitle:#"Close" otherButtonTitles:nil];
[message show];
}
#end
I am not sure what I am doing wrong, or why the app keeps stalling at the loading screen, please help!
I found this in the debugger
2012-12-01 19:39:03.428 MindTrip[3327:11303] * Terminating app due
to uncaught exception 'NSUnknownKeyException', reason:
'[ setValue:forUndefinedKey:]: this class is
not key value coding-compliant for the key Alert.'
* First throw call stack: (0x1c9a012 0x10d7e7e 0x1d22fb1 0xb84711 0xb05ec8 0xb059b7 0xb30428 0x23c0cc 0x10eb663 0x1c9545a 0x23abcf
0xffe37 0x100418 0x100648 0x100882 0x4fa25 0x4fdbf 0x4ff55 0x58f67
0x1cfcc 0x1dfab 0x2f315 0x3024b 0x21cf8 0x1bf5df9 0x1bf5ad0 0x1c0fbf5
0x1c0f962 0x1c40bb6 0x1c3ff44 0x1c3fe1b 0x1d7da 0x1f65c 0x2a4d 0x2975)
libc++abi.dylib: terminate called throwing an exception (lldb)
The issue is that you have added alert(or a pointer to some view called alert) in your storyboard but the same is not added to the class code. If you are displaying the alert view programmatically, you can remove it from the storyboard.

IOS. Unable to close view after clicking my Info button (newbie)

I have found some code for my infoButton which shows up my new credits.xib but I can't manage to come back to my RootViewController.
On my Credits.xib, I have linked my "Done" button with ToucheDown-FirstResponder-ToggleCredits Close.
Here is my code for the infoButton in my RootViewController.m in ViewDidLoad
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(toggleCreditsOpen:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];
//[button release];
[modalButton release];
and my code just after my ViewDidLoad
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"Credits" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
//[self.navigationController dismissModalViewControllerAnimated:YES];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
I think I am missing something should I create a Credits.h and put the toggleCreditsClosed in it ?
Here is the stack trace
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7c67610> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key DoneButton.'
*** First throw call stack:
Here is my Credits.h
#import <UIKit/UIKit.h>
#interface Credits : UIViewController
{
IBOutlet UIButton *DoneButton;
}
#property (nonatomic, retain) UIButton *DoneButton;
#end
and my Credits.m
#import "Credits.h"
#implementation Credits
#synthesize DoneButton;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
[self dismissModalViewControllerAnimated:YES];
}
#end
If a delete the DoneButton link, the Credits view shows up but I it's when I press the Done button that I have a problem
Costumes[402:11f03] -[UIViewController toggleCreditsClosed:]: unrecognized selector sent to instance 0x7a4c460
2012-10-24 22:19:33.271 Costumes[402:11f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController toggleCreditsClosed:]:
Sorry but I don't understand what I have to do and I cannot upload an image to show you but
in Outlets the link is (view<->View) and in Received Actions (toggleCreditsClosed:<->Button-Done Touch Down)
Welcome on SO !
Yes, you should create a separate .h / .m for your Credits. Then tell Inteface Builder that your .xib is a Credits class. Then link your button to this .h with the action you want. Basically, your last method should be in Credits.m :
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
[self dismissModalViewControllerAnimated:YES];
}
Watch out, you dismiss the modal view with self instead of self.parentViewController in your code !
(PS : answers you'll get may not always work. Don't hesitate to comment, telling us what (didn't) work !)

"EXC_BAD_ACCESS" when switch too fast between an tableView and a mapView

I have a tableView with an button to push a mapView. The push and back actions generally work fine. If I switch quickly between these two views, "EXC_BAD_ACCESS" error will appear.
MapViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
UIButton *btnL = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0, 40.0)];
[btnL setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[btnL addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btnL] autorelease];
[btnL release];
self.whereAmIAnnotation = [[[WhereAmIAnnotation alloc] init] autorelease];
if (!self.mapView || !self.whereAmIAnnotation) {
NSLog(#"mapview : %#", self.mapView);
NSLog(#"whereAmIAnnotation : %#",self.whereAmIAnnotation);
// will never enter in to here
}
[self.mapView addAnnotation:self.whereAmIAnnotation];
}
If I comment [self.mapView addAnnotation:self.whereAmIAnnotation]; , there is no "EXC_BAD_ACCESS" anymore.
Any answers and comments will be appreciated. Thanks in advance!
Edit 2
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
// "EXC_BAD_ACCESS" error message shows here
}
}
Edit 3:
the whereAmIAnnotation is declared here:
MapViewController.m
#interface AddrCoordinateAdjustMapViewController()
#property (retain) WhereAmIAnnotation *whereAmIAnnotation;
#end
#implementation MapViewController
#synthesize whereAmIAnnotation;
Edit 4:
Error message is as following:
2012-07-30 15:56:19.735 myApp[13584:707] *** -[MapViewController respondsToSelector:]: message sent to deallocated instance 0x10195e80
It usually crashes when i switch back to the tableView while the annotationView is dropping.
Have you removed yourself as delegate of the mapview?
self.mapview.delegate = nil;
try removing it at viewWilldisappear or whenever you consider necessary as you might need it later if you have pushed a view controller and will return to the view later.
In a nutshell, remove it when your view cannot respond to the delegate which is why you got the EXC_BAD_ACCSS. it sent a message to your view but it was already released from memory.
Do you release and set to nil all your IBOutlets in the viewDidUnload? So at least you should do:
- (void)viewDidUnload {
self.mapView = nil;
[super viewDidUnload];
}
Generally in ViewDidUnload you should release and set to nil, any view objects that are created from a Nib file or allocated in your ViewDidLoad method
I would suggest to check whether self.whereAmIAnnotation isnt already released by at the time you add it to mapView. That might be one reason for the BAD_ACCESS you receive.
I had similar problem and my solution was to remove all overlays from the map before popping controller from UINavigationController. I was using OpenStreetMap.

Creating an iPhone contact in Objective-C

I am trying to create a contact in the internal address book. My code is as follows:
in the CreateNewAccountPage.h file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
....
#interface CreateNewAccountPage : UIViewController <UIScrollViewDelegate,
UITextViewDelegate, MainMenuDelegate, ABNewPersonViewControllerDelegate> {
....
}
- (IBAction)goToContactCreation;
...
#end
In the CreateNewAccountPage.m file:
#import "CreateNewAccountPage.h"
#implementation CreateNewAccountPage
-(void)showNewPersonViewController
{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentModalViewController:navigation animated:YES];
[picker release];
[navigation release];
}
-(IBAction)goToContactCreation {
[self showNewPersonViewController];
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self dismissModalViewControllerAnimated:YES];
}
...
#end
The error that I am getting is:
"_OBJC_CLASS_$_ABNewPersonViewController", referenced from:
objc-class-ref-to-ABNewPersonViewController in CreateNewAccountPage.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
If I comment out the lines:
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
...
[picker release];
I no longer receive the error. I am just trying to get a contact to be created. It doesn't have to do with accessing the contact list. I just want a button to create the contact.
Thanks so much!
Add AddressBook.framework, AddressBookUI.framework frameworks to your project.