UIImagePickerController get image in an inherited - iphone

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.

Related

How to change my profile picture image when I click on done button the image will be change in first view?

How to change my profile picture image, when I click on done button in second screen the first screen image should change with the second screen image.
I am selecting image from Gallery that image was appearing in my second screen how to set this image in first screen view controller while I am clicking on done button.
This is MprofileViewController.h
#import <UIKit/UIKit.h>
#import "AddProfileViewController.h"
#class MProfileViewController;
#interface MProfileViewController : UIViewController<UIImagePickerControllerDelegate,UITableViewDataSource,UITableViewDelegate,ImageSelectionDelegate>
{
NSMutableArray* titles;
IBOutlet UITableView *mainTableView;
IBOutlet UIImageView *image2;
}
#property(strong,nonatomic)IBOutlet UIImageView *image2;
#property(nonatomic, retain) NSMutableArray *titles;
#property(strong,nonatomic)UITableView *mainTableView;
-(IBAction) clickEventOnImage:(id) sender;
#end
MprofileViewController.m
#import "MProfileViewController.h"
#interface MProfileViewController ()
#end
#implementation MProfileViewController
#synthesize titles,mainTableView;
#synthesize image2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
titles=[[NSMutableArray alloc]init];
self.navigationItem.title = #"View Profile";
image2.image=[UIImage imageNamed:#"hariku-indah.jpg"];
}
- (void) imageSelected:(UIImage *)image {
// Use image
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction) clickEventOnImage:(id) sender{
AddProfileViewController *Avc = [[AddProfileViewController alloc]initWithNibName:#"AddProfileViewController" bundle:nil];
Avc.delegate=self;
[self.navigationController pushViewController:Avc animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
}
#end
AddProfileViewController.h
#import <UIKit/UIKit.h>
#protocol ImageSelectionDelegate <NSObject>
- (void) imageSelected:(UIImage*)image;
#end
#interface AddProfileViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>{
IBOutlet UIImageView *imageView;
NSData *dataImage;
}
// Delegate property
#property (nonatomic,assign) id<ImageSelectionDelegate> delegate;
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)back:(id)sender;
-(IBAction)done:(id)sender;
#end
AddprileViewController.m
#import "AddProfileViewController.h"
#import "MProfileViewController.h"
#interface AddProfileViewController ()
#property(strong,nonatomic) UIImagePickerController *imagePicker;
#end
#implementation AddProfileViewController
#synthesize imageView;
#pragma mark -
#pragma mark View lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"actionSheet");
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0) {
[self pushTakePhotoScreenInDelegate];
}
else if (buttonIndex == 1) {
[self pushChoosePhotoScreenInDelegate];
}
}
-(void)pushTakePhotoScreenInDelegate
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"viewfinder_2.png"]];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
[imageView setFrame:CGRectMake(0, -52/2.0, screenSize.width, screenSize.height)];
self.imagePicker.cameraOverlayView = imageView;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
-(void)pushChoosePhotoScreenInDelegate
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
//-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
self.imageView.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
// In case you are using image picker, this delegate is called once image selection is complete.
//- (void)imagePickerController:(UIImagePickerController *)picker
//didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//Use either according to your setting, whether you allow image editing or not.
//self.imageView.image = image;
//UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
//For edited image
// UIImage *myImage = [info objectForKey:UIImagePickerControllerOriginalImage];
//if([_delegate respondsToSelector:#selector(imageSelected:)]) {
// [self.delegate imageSelected:myImage];
// }
//}
/*
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
AddProfileViewController * controller = [AddProfileViewController new];
controller.imageView.image = pickedImage;
// [self.navigationController pushViewController:controller animated:YES];
}*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..."
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
}
-(IBAction)back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)done:(id)sender
{
// if([_delegate respondsToSelector:#selector(imageSelected:)]) {
// [self.delegate imageSelected:imageView];
// }
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES];
}
#end
// if([_delegate respondsToSelector:#selector(imageSelected:)]) {
// [self.delegate imageSelected:imageView]; }
This code is technically correct. But you forgot to synthesize your delegate in AddprofileViewController

NSString from other UIViewController is null

Could anyone please explain to me why this isn't working? I think it is a real simple problem, but for some reason I am not getting it.
My problem: I have a NSString in my firstViewController with value "Hello!". Now I want to display this value in my secondViewController but the output is null.
This is my NSLog
2013-03-09 foo[95381:c07] value of foo in BITfirstViewcontroller: Hello!
2013-03-09 foo[95381:c07] value of foo in BITsecondViewcontroller: (null)
BITAppDelegate.m
#import "BITAppDelegate.h"
#import "BITfirstViewController.h"
#implementation BITAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BITfirstViewController *fvc = [[BITfirstViewController alloc]init];
// Create navigationController and set InputViewController as root for navController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fvc];
[self.window setRootViewController:navController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
BITfirstViewController.h
#import <UIKit/UIKit.h>
#interface BITfirstViewController : UIViewController
#property (strong, nonatomic) NSString *foo;
-(IBAction)showSecondView:(id)sender;
#end
BITfirstViewController.m
#import "BITfirstViewController.h"
#import "BITsecondViewController.h"
#interface BITfirstViewController ()
#end
#implementation BITfirstViewController
#synthesize foo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
foo = [NSString stringWithFormat:#"Hello!"];
NSLog(#"value of foo in BITfirstViewcontroller: %#",foo);
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(showSecondView:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)showSecondView:(id)sender;
{
BITsecondViewController *svc = [[BITsecondViewController alloc]init];
[self.navigationController pushViewController:svc animated:YES];
}
#end
BITsecondViewController.h
#import <UIKit/UIKit.h>
#class BITfirstViewController;
#interface BITsecondViewController : UIViewController
#property (nonatomic, retain) BITfirstViewController *fvc;
#end
BITsecondViewController.m
#import "BITsecondViewController.h"
#import "BITfirstViewController.h"
#interface BITsecondViewController ()
#end
#implementation BITsecondViewController
#synthesize fvc;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"value of foo in BITsecondViewcontroller: %#", fvc.foo);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this in BITfirstViewController.m
-(IBAction)showSecondView:(id)sender;
{
BITsecondViewController *svc = [[BITsecondViewController alloc]init];
svc.fvc = self; // you need to set this property as you prepare the svc
[self.navigationController pushViewController:svc animated:YES];
}
EDIT:
Your fvc and svc are encapsulated, and don't have direct knowledge about each-other except though the use of properties (at least that's the way we try to do things in OOP). So, you were correct in creating a #property in your svc that allowed you to have a handle to your fvc, but [[BITsecondViewController alloc]init] will simply initialize that pointer to nil. Then you need to set that property to point to the intended object, which in this case is the fvc. Since the svc is being build from inside an instance of BITFirstViewController, the correct handle to pass in is self.

Can't dismiss modal view when tap a button

In "FirstViewController" I declare a button which present the modal view "InfoViewController".
In "InfoViewController", I declare a toolbar with a "modalViewButton" UIButton which dismiss the modal view. But the "OK" UIButton doesn't work. I don't know why.
Here's FirstViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface FirstViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
Here's FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize infoViewController;
- (IBAction)modalViewAction:(id)sender
{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[infoViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self
action:#selector(modalViewAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.leftBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Here's InfoViewController.h
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController
{
}
-(IBAction)infoDismissAction:(id)sender;
#end
Here's the InfoViewController.m
#import "InfoViewController.h"
#implementation InfoViewController
- (IBAction)infoDismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *infoLabel = [[UILabel alloc] init];
infoLabel.frame = CGRectMake(50, 100, 100, 40);
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.text = #"About";
[self.view addSubview:infoLabel];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"OK"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(infoDismissAction:)];
UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:#"About"
style:UIBarButtonItemStylePlain
target:self action:nil];
NSArray *barButtons = [[NSArray alloc] initWithObjects:flexibleSpace,flexibleSpace,infoTitle,flexibleSpace,doneButton,nil];
[toolBar setItems:barButtons];
[self.view addSubview:toolBar];
[toolBar release];
[infoTitle release];
[doneButton release];
[barButtons release];
[infoLabel release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I would solve this issue with a delegate method.
First make a protocol in your modalViewController
#protocol ModalViewDelegate <NSObject>
- (void)didDismissModalView;
#end
And set a delegate property in the same modalVC:
id<ModalViewDelegate> dismissDelegate;
Then make a buttonActionMethod that calls the delegate in the modalVC:
- (void)methodCalledByButton:(id)sender
{
// Call the delegate to dismiss the modal view
[self.dismissDelegate didDismissModalView];
}
Now your modalVC is done you have to prepare the mainVC calling the modalVC:
You have to make your MainViewController comform to the delegate:
#interface MainViewController : UIViewController <ModalViewDelegate>
At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:
self.myModalViewController.dismissDelegate = self;
Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.
-(void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)
Hope this all makes sense.
Good luck.
You can only dismiss currently displayed modal view, so in your method infoDismissAction: you should do one of following
1) [self dismissModalViewControllerAnimated:YES];
2) Send to parent view controller message that current modal view should be dismissed and send reference to that view.
Second approach is better as it is more safe.
In your -infoDismissAction try to call [self dismissModalViewControllerAnimated:YES];
Here the best sample code for the model view for iphone and ipad also.
The popups have a number of configurable items. They can be animated to either slide or popup onto the display. Once visible, they can be dismissed either by tapping the screen or after a programmed delay. The background and text colors can also be adjusted however you like.
Download the sample code from here.
The current answers are deprecated. Here is the updated code:
[self dismissViewControllerAnimated:NO completion:nil];

Why Isn't My Info Button Working?

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];

Problem with Landscape and Portrait view in a TabBar Application

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;
}