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];
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.
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.
I have the following code:
MapViewController.h
#import <UIKit/UIKit.h>
#import "PlaceViewController.h"
#interface MapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet PlaceViewController *placeview;
}
- (IBAction)passPlace:(id)sender;
MapViewController.m
#synthesize placeview;
- (IBAction)passPlace:(id)sender{
self.placeview = [[[PlaceViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[self presentViewController:placeview animated:YES completion:nil];
}
PlaceViewController.h
#interface PlaceViewController : UIViewController{
}
- (IBAction)back:(id)sender;
#end
PlaceViewController.m
#implementation PlaceViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"Change!!!!!");
[super viewDidLoad];
// Do any additional setup after loading the view.
}
And the storyboard:
http://img685.imageshack.us/img685/1233/screengy.png
I have no idea that what I'm doing wrong because when I change view, the PlaceViewController is a black screen with a blue status bar.
Anyone can help me? Thanks
if you are using storyboard... go into the Storyboard Editor and in the Attributes Inspector, set an Identifier, let say "PlaceMap" (in picture “AddPlayer”)
change your code:
- (IBAction)passPlace:(id)sender{
self.placeview = [[[PlaceViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[self presentViewController:placeview animated:YES completion:nil];
}
with
- (IBAction)passPlace:(id)sender{
PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:#"PlaceMap"];
[self.navigationController pushViewController:newView animated:YES];
}
You can check alos, a very nice tutorial: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
UPD
that is for Navigation Controller
[self presentViewController:placeview animated:YES completion:nil];
if you want modal... can you use:
newView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//deprecated 6.0
// [self presentModalViewController:newView animated:YES];
//from 5.0
[self presentViewController:newView animated:YES completion:NULL];
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];
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;
}