My iPhone app is quite simple, however it errors out. My app delegate opens up this view controller:
#import "Launch.h"
#implementation Launch
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonPressed {
NSLog(#"Button Pressed!");
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
//image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
which is just a red screen with a button. When I click the button, the app errors out with no error message. Why?
here is my main.m method, and when the program errors out, it points to the "return" line and indicates an EXEC_BAD_ACCESS in a thread.
#import <UIKit/UIKit.h>
#import "testAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([testAppDelegate class]));
}
}
The target class has been deallocated, it needs to be retained.
From the Apple docs:
addTarget:action:forControlEvents:
When you call this method, target is not retained.
You need to insure the class instance is retained.
Change it to:
#selector(buttonPressed:) //add the :
and use this:
-(IBAction)buttonPressed:(id)sender;
The button is asking for an action.
Related
I searched the internet for a solution but unfortunately I'm not able to find a solution to my problem.
Most people are creating an object of the UIImagePickerController class but I decided to do this on different way.
I created a new class CameraViewController this class extends of the UIImagePickerController.
My .h file:
#import <UIKit/UIKit.h>
#interface CameraViewController : UIImagePickerController <UIImagePickerControllerDelegate>
-(void)captureImage;
#end
My .m file:
#import "CameraViewController.h"
#define CAMERA_TRANSFORM_X 1
#define CAMERA_TRANSFORM_Y 1.5
#interface CameraViewController ()
#end
#implementation CameraViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
UIButton *btnCapture = [[UIButton alloc] initWithFrame:CGRectMake((320/2)-(76/2), 480, 76, 76)];
[btnCapture setImage:[UIImage imageNamed:#"cam.png"] forState:UIControlStateNormal];
[btnCapture addTarget:self action:#selector(captureImage) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnCapture];
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.wantsFullScreenLayout = YES;
self.cameraViewTransform = CGAffineTransformScale(self.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
self.cameraOverlayView = view;
//[self openCamera];
// Do any additional setup after loading the view.
}
-(void)takePicture //overwrite take picture method
{
[super takePicture];
NSLog(#"%#", self);
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"%#", info);
}
-(void)captureImage
{
[self takePicture];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I implemented the delegate UIImagePickerControllerDelegate. But it seems that my imagePickerController is not called for some reason. In this method I try to log a NSDirectory. So I thought maybe I can get my image by overwriting the takePicture method, but I have no idea how. Anyone know how to help me? Thanks a lot
From apple docs The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.
So you can't subclass UIImagePickerController.
ive created coverflow effect using icarousel,but i've added two buttons not by programmatically in xib and i defined outlet and action and connected for that buttons..but those buttons are displayed as image and not working...Here is my code in .m file
#interface GoldViewController ()<UIActionSheetDelegate>
#property (nonatomic, assign) BOOL useButtons;
#end
#implementation GoldViewController
#synthesize carousel1;
-(IBAction)showhome:(id)sender{
ViewController *sec=[[ViewController alloc]initWithNibName:Nil bundle:Nil];
sec.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:sec animated:YES];
}
-(IBAction)showback:(id)sender{
CollectionsViewController *sec=[[CollectionsViewController alloc]initWithNibName:Nil bundle:Nil];
sec.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:sec animated:YES];
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
carousel1.type = iCarouselTypeCylinder;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return NUMBER_OF_ITEMS;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:#"ban.jpg"],
[UIImage imageNamed:#"pen.jpg"],
[UIImage imageNamed:#"ear1.jpg"],
[UIImage imageNamed:#"neck.jpg"],
[UIImage imageNamed:#"brac.jpg"],
[UIImage imageNamed:#"rings.jpg"],nil];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 250.0f, 320.0f);
[button setImage:(UIImage*)[buttonImage objectAtIndex:index] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
return ITEM_SPACING;
}
- (BOOL)carousel:(iCarousel *)_carousel shouldSelectItemAtIndex:(NSInteger)index
{
if (index == carousel1.currentItemIndex)
{
NSLog(#"Should select current item");
}
else
{
NSLog(#"Should select item number %i", index);
}
return YES;
}
- (void)carousel:(iCarousel *)_carousel didSelectItemAtIndex:(NSInteger)index
{
if (index == carousel1.currentItemIndex)
{
//note, this will only ever happen if useButtons == NO
//otherwise the button intercepts the tap event
NSLog(#"Did select current item");
}
else
{
NSLog(#"Did select item number %i", index);
}
}
#end
Make sure that the nib files owner is definitely GoldViewController. Check that you've definitely added the buttons to the nib as buttons and not images. Make sure touchupinside action on each relevant button is connected to the showhome and showback actions in the nib files owner.
If all of this is ok, its possible that when the view controllers view is being displayed, the iCarousel's view is overlaying your button views. (I seem to recall some issues with iCarousel not laying out the view at runtime to be in the same place as defined in the nib but don't remember details). To find out - log the frame of the buttons and of the iCarousel view in your viewWillAppear method.
I have an app where user can take picture from camera and image is displayed in imageview.
It works fine for the first time but the second time app is crashed leaving memory warning level=1
I google around and tried all possible ways but no use
Below is the code i used in my app
UIImagePickerController *imgpicker;
#property(nonatomic,retain) UIImagePickerController *imgpicker;
#synthesize imgpicker;
if(actionSheet.tag==1)
{
NSLog(#"button index:%i",buttonIndex);
if(buttonIndex == 0)
{ NSLog(#"button index for camera:%i",buttonIndex);
if(self.imgpicker==nil){
self.imgpicker=[[UIImagePickerController alloc]init];
self.imgpicker.delegate =self;
self.imgpicker.sourceType=UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:self.imgpicker animated:YES];
[imgpicker release];
return;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"didFinishPickingMediaWithInfo editing info:%#",info);
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];
NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);
[imageview setImage:[UIImage imageWithData:newjpg]];
[scrollView addSubview:imageview];
[self.view addSubview:scrollView];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Please help me
Try changing this:
self.imgpicker=[[UIImagePickerController alloc]init];
to this:
self.imgpicker=[[[UIImagePickerController alloc]init] autorelease];
and remove this line:
[imgpicker release];
If you're showing the image picker more than once, you're leaking memory from the image view that gets allocated every time the picker finishes.
You're also adding the image view to the scroll view and the scroll view to the view controller's view every time. You just need to do it once, and the update the image property on the image view.
Try this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"didFinishPickingMediaWithInfo editing info:%#",info);
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
if (imageview == nil) {
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(65, 10, 75, 75)];
[scrollView addSubview:imageview];
[self.view addSubview:scrollView];
}
NSData *newjpg = UIImageJPEGRepresentation(image, 0.5);
[imageview setImage:[UIImage imageWithData:newjpg]];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Please create UIImagePickerController object in your AppDelegate class and initialize in your AppDidFinishloading method : didFinishLaunchingWithOptions. Realese it on appDelegate dealloc method.
You can access it in your needed view controller Page from appdelegate class.
Please do not create evertytime UIImagePickerController object when u needed and realese it.
This might help you.
Please use this code :
App Delegate
// .h
#import <UIKit/UIKit.h>
#interface MyProjectAppDelegate : NSObject <UIApplicationDelegate> {
UIImagePickerController *image_picker;
UIWindow *window;
UINavigationController *navigationController;
}
#property(nonatomic, retain) UIImagePickerController *image_picker;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
-(void)initializeImagePickerView;
#end
// .m
#import "MyProjectAppDelegate.h"
#import "RootViewController.h"
#implementation MyProjectAppDelegate
#synthesize window;
#synthesize navigationController,image_picker;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self initializeImagePickerView];
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
-(void)initializeImagePickerView
{
// Picker View
self.image_picker = [[UIImagePickerController alloc] init];
self.image_picker.allowsImageEditing = YES;
}
- (void)dealloc {
[image_picker release];
[navigationController release];
[window release];
[super dealloc];
}
#end
//RootView Controller
// .h
#import <UIKit/UIKit.h>
#import "MyProjectAppDelegate.h"
#interface RootViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
MyProjectAppDelegate *appDelegate;
}
-(IBAction)btnPhotoPressed:(id)sender;
#end
// .m
#import "RootViewController.h"
#implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (MyProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(IBAction)btnPhotoPressed:(id)sender
{
appDelegate.image_picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
appDelegate.image_picker.delegate=self;
[self presentModalViewController:appDelegate.image_picker animated:YES];
}
#pragma mark UIImagePickerController delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *imgCapturePhoto = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// You can use image here
[self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[super dealloc];
}
This will definitely help u.
My Info button is showing up when I run my app but it doesn't do anything, no response when I click it.
In my ProfileViewController file:
- (void)viewDidLoad
{
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
[super viewDidLoad];
}
I also have the following two methods to load the about view (the screen that loads up when the button is clicked):
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
EDIT:
I am adding my full implementation file here:
#import "ProfileViewController.h"
#import "AboutViewController.h"
#implementation ProfileViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (IBAction)toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction)toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoDark] retain];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
//[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:infoButton];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Using your provided code works for me, if self.navigationController is non-nil, and there exists an AboutViewController.xib in your main bundle. Otherwise, I don't see anything readily wrong.
If you don't have a navigation controller, the proper line in toggleCreditsOpen: would be:
[self presentModalViewController:theController animated:YES];
Edit:
If you want to dismiss the modal view later, its usually a good idea to do this with a delegate. Instead of
UIViewController *theController = [[UIViewController alloc] initWithWhatever];
you can do
AboutViewController *theController = [[AboutViewController alloc] initWithWhatever];
where AboutViewController has a delegate. Probably something like id<DismissModalProtocol> delegate;. Then, your view controller would implement this #protocol, and before it calls presentModalViewController:animated:, it'll set itself as the delegate. So very roughly, it would look something like this:
// AboutViewController.h
#protocol DismissModalProtocol;
#interface AboutViewController : UIViewController {
id<DismissModalProtocol> delegate;
}
#property id<DismissModalProtocol> delegate;
#end
#protocol DismissModalProtocol <NSObject>
- (void)dismissController:(UIViewController *)viewController;
#end
// ProfileViewController.h
#import "AboutViewController.h"
#interface ProfileViewController : UIViewController <DismissModalProtocol>
#end
// ProfileViewController.m
#implementation ProfileViewController
- (void)dismissController:(UIViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
- (void)toggleCreditsOpen:(id)sender {
AboutViewController *controller = [[AboutViewController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
#end
I don't know at all why this isn't working, but if I were to take a guess, I'd say it's in the [self.navigationController presentModalViewController:theController animated:TRUE];
statement. From what I know, it should be:
[self.navigationController presentModalViewController:theController animated:YES];
See if it works after you do that. I'm not sure, but that could be the problem with your code.
hi
you are took a mistake to define your button..
just assign retain property at the end of button
like
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] retain];
and also give
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];
I have an TabBar app that I would like to be in landscape and portrait. The issue is when I go to tab 2 make a selection from my table then select tab 1 and rotate the device, then select tab 2 again the content does not know that the device rotated and will not display my custom orientated content correctly. I am trying to write a priovate method that tells the view what orientation it is currently in. IN viewDidLoad I am assuming it is in portrait but in shouldAutoRotate I have it looking in the private method for the correct alignment of the content. Please Help!! Here is my code:
#import "DetailViewController.h"
#import "ScheduleTableViewController.h"
#import "BrightcoveDemoAppDelegate.h"
#import "Constants.h"
#implementation DetailViewController
#synthesize CurrentLevel, CurrentTitle, tableDataSource,logoName,showDescription,showDescriptionInfo,showTime, showTimeInfo, tableBG;
- (void)layoutSubviews {
showLogo.frame = CGRectMake(40, 20, 187, 101);
showDescription.frame = CGRectMake(85, 140, 330, 65);
showTime.frame = CGRectMake(130, 10, 149, 119);
tableBG.frame = CGRectMake(0, 0, 480, 320);
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = CurrentTitle;
[showDescription setEditable:NO];
//show the description
showDescription.text = showDescriptionInfo;
showTime.text = showTimeInfo;
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *ImagePath = [Path stringByAppendingPathComponent:logoName];
UIImage *tempImg = [[UIImage alloc] initWithContentsOfFile:ImagePath];
[showLogo setImage:tempImg];
[tempImg release];
[self masterView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
isLandscape = UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
if(isLandscape = YES){
[self layoutSubviews];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[logoName release];
[showLogo release];
[showDescription release];
[showDescriptionInfo release];
[super dealloc];
}
#end
I believe that you need to pass the -shouldAutorotateToInterfaceOrientation: method to each view controller. You could do something like this in your UITabBarController:
- ( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )
interfaceOrientation
{
for ( UIViewController *viewController in [ self viewControllers ])
[ viewController
shouldAutorotateToInterfaceOrientation:interfaceOrientation ];
return YES;
}