insertSubView: is not showing my views - iphone

I am stuck in a tutorial, trying to switch views with a toolbar. My code builds fine and I put some alerts in to check the toolbar was working, and it is. But my subviews I am inserting are just not showing up at all. Can anybody help please?
Here is my main controller code..
MainViewController.h is
#import
#class FlavourPickerView;
#class ShakeView;
#class ResultsView;
#interface MainViewController : UIViewController {
IBOutlet FlavourPickerView *flavourPickerView;
IBOutlet ShakeView *shakeView;
IBOutlet ResultsView *resultsView;
}
#property (retain,nonatomic) FlavourPickerView *flavourPickerView;
#property (retain,nonatomic) ShakeView *shakeView;
#property (retain,nonatomic) ResultsView *resultsView;
-(IBAction)loadFlavourPickerView:(id)sender;
-(IBAction)loadShakeView:(id)sender;
-(IBAction)loadResultsView:(id)sender;
-(void)clearView;
#end
and here is MainViewController.m
#import "MainViewController.h"
#import "FlavourPickerView.h"
#import "ShakeView.h"
#import "ResultsView.h"
#implementation MainViewController
#synthesize flavourPickerView;
#synthesize shakeView;
#synthesize resultsView;
-(IBAction)loadFlavourPickerView:(id)sender{
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc]
initWithTitle:#"Flavour selected"
message:#"Seems to work"
delegate:nil
cancelButtonTitle:#"OK!"
otherButtonTitles:nil];
[alertDialog show];
[alertDialog release];
[self clearView];
[self.view insertSubview:flavourPickerView.view atIndex:10];
}
-(IBAction)loadShakeView:(id)sender{
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc]
initWithTitle:#"Shaker selected"
message:#"Seems to work"
delegate:nil
cancelButtonTitle:#"OK!"
otherButtonTitles:nil];
[alertDialog show];
[alertDialog release];
[self clearView];
[self.view insertSubview:shakeView.view atIndex:0];
}
-(IBAction)loadResultsView:(id)sender{
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc]
initWithTitle:#"Results selected"
message:#"Seems to work"
delegate:nil
cancelButtonTitle:#"OK!"
otherButtonTitles:nil];
[alertDialog show];
[alertDialog release];
[self clearView];
[self.view insertSubview:resultsView.view atIndex:0];
}
-(void)clearView{
if(flavourPickerView.view.superview){
[flavourPickerView.view removeFromSuperview];
}else if(shakeView.view.superview){
[shakeView.view removeFromSuperview];
}else{
[resultsView.view removeFromSuperview];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[self loadFlavourPickerView:nil];
[super viewDidLoad];
}
- (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 {
[flavourPickerView release];
[shakeView release];
[resultsView release];
[super dealloc];
}
#end
and here is an example of one of the other views, starting with FlavourPickerView.h
#import
#interface FlavourPickerView : UIViewController {
}
#end
and FlavourPickerView.m
#import "FlavourPickerView.h"
#implementation FlavourPickerView
- (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 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
There is also a nib for the flavourPickerView and I have added subviews into my mainView, with the Class set to FlavourPickerView and nib name is FlavourPickerView.xib.

Are flavourPickerView, shakeView, and resultsView properly initialized when you add their view to this UIViewController view?
Another thing: avoid to call a UIViewController just 'XXXView', try instead 'XXXViewController'

Related

How to show GADInterstitial in ios project?

How to show GADInterstitial in ios project? i am getting error "Request error : no ads to show" but admob is working in my another project.
// ViewController.h
#import <UIKit/UIKit.h>
#import "GADBannerView.h"
#import "GADCustomEventBanner.h"
#import "GADInterstitial.h"
#interface ViewController : UIViewController <GADInterstitialDelegate>
{
UIButton *interstitialButton_;
GADInterstitial *interstitial_;
}
#property(nonatomic, retain) IBOutlet UIButton *interstitialButton;
#property(nonatomic, retain) GADInterstitial *interstitial_;
- (IBAction)showInterstitial:(id)sender;
#end
//ViewController.h
#import "ViewController.h"
#implementation ViewController
#synthesize interstitialButton ;
#synthesize interstitial_;
- (void)dealloc {
interstitial_.delegate = nil;
[interstitial_ release];
[interstitialButton_ release];
[super dealloc];
}
- (void)interstitial:(GADInterstitial *)interstitial
didFailToReceiveAdWithError:(GADRequestError *)error {
// Alert the error.
UIAlertView *alert = [[[UIAlertView alloc]
initWithTitle:#"GADRequestError"
message:[error localizedDescription]
delegate:nil cancelButtonTitle:#"Drat"
otherButtonTitles:nil] autorelease];
[alert show];
interstitialButton_.enabled = YES;
}
-(void)interstitialWillPresentScreen:(GADInterstitial *)ad{
NSLog(#"on screen");
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
[interstitial presentFromRootViewController:self];
interstitialButton_.enabled = YES;
[self showInterstitial:nil];
}
- (IBAction)showInterstitial:(id)sender {
self.interstitial_ = [[[GADInterstitial alloc] init] autorelease];
self.interstitial_.delegate = self;
self.interstitial_.adUnitID = MY_ADMOB_KEY; // this key is working in admob //
GADRequest *request = [GADRequest request];
request.testing = YES;
[self.interstitial_ loadRequest: request];
interstitialButton_.enabled = NO;
}
#end
Change the ad size to full screen in the settings in admob website.
self.interstitial_.adUnitID = MY_ADMOB_KEY; // this key is working in admob
check this comment, and replace it with:
self.interstitial_.adUnitID = #"ca-app-pub-xxxxxxxxxxx"; // this key is working in admob
you should get you own adUnitID form admob

How to send mail in xcode using ARC

Okay after finding a couple of tutorials online. Everything is okay except when the button is being pressed. I pressed the button that will sent an email and suddenly there is a signal abort. I watched two videos and re type every code in each video twice. Why is there a signal abort? Please help. Thank you!
this is the .h file
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface secondViewController : UIViewController <MFMailComposeViewControllerDelegate> {
IBOutlet UILabel *resultLabel;
}
-(IBAction)switchToFirst:(id)sender;
-(IBAction)openMail:(id)sender;
#end
this is the .m file
#import "secondViewController.h"
#import "ViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
-(IBAction)openMail:(id)sender {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"", nil]];
[composer setSubject:#""];
[composer setMessageBody:#"message" isHTML:YES];
[composer setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:composer animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:#"Can't send your email!" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultSent:
resultLabel.text = #"Mail was sent";
break;
case MFMailComposeResultSaved:
resultLabel.text = #"Mail was saved to drafts";
break;
case MFMailComposeResultFailed:
resultLabel.text = #"Mail wasn't able to sent";
break;
case MFMailComposeResultCancelled:
resultLabel.text = #"Mail was Cancelled";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchToFirst:(id)sender {
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
- (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 from its nib.
// Set the background image by setting the contents of the view's layer
UIImage *bg = [UIImage imageNamed:#"iPad iDeaf Assisstant Background.png"];
self.view.layer.contents = (id) [bg CGImage];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
If you have a class for email sending you can compile it without arc, just set flag -fno-objc-arc for files in project settings->build phases
Like #qegal said, just take out all the retains and releases.
And if you want to keep the code you have, you can just mark files as non-arc.
To mark any file as non-arc you can set a compiler flag in the build phases. Select your project in the files pane on the left hand side. Then select your target, go to build phases, and double click the column underneath compiler flags for the file you want to mark and enter 'fno-objc-arc'.

iPhone Objective C - can't display new view

I'm finding it difficult to get started in Objective-C.
I'm executing the following code when a button is clicked:
NSLog(#"hi");
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
I can see "hi" in the console when I hit the button, it's just that the view should change to MainMenuDriver. But nothing happens!
Please help!
As per request for more code:
MainMenuDriver.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MainMenuDriver : UIViewController <UINavigationControllerDelegate,CLLocationManagerDelegate>{
IBOutlet UIButton *photos;
IBOutlet UIButton *profile;
IBOutlet UISwitch *onOffline;
IBOutlet UILabel *label1;
NSTimer *uploadGPS_timer;
CLLocationManager *lm;
NSString *lat;
NSString *lng;
}
#property(nonatomic,retain) UIButton *photos;
#property(nonatomic,retain) UIButton *profile;
#property(nonatomic,retain) UISwitch *onOffline;
#property(nonatomic,retain) UILabel *label1;
#property (nonatomic,retain) NSTimer *uploadGPS_timer;
#property(nonatomic,retain) NSString *lat,*lng;
-(IBAction)showMessages:(id)sender;
-(IBAction)showFriends:(id)sender;
-(IBAction)showPhotos:(id)sender;
-(IBAction)showProfile:(id)sender;
-(IBAction)switchSwitched:(id)sender;
-(void)uploadGPS_tick:(NSTimer*)timer;
#end
MainMenuDriver.m
#import "MainMenuDriver.h"
#import "ASIFormDataRequest.h"
#import "JoeMaxiViewController.h"
#import "Photos.h"
#import "Profile.h"
#implementation MainMenuDriver
#synthesize messages,profile,photos,friends,label1;
#synthesize onOffline;
#synthesize uploadGPS_timer;
#synthesize lat,lng;
-(IBAction)showPhotos:(id)sender{
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
Photos *x= [[Photos alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:YES];
}
-(IBAction)showProfile:(id)sender{
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil]];
Profile *x= [[Profile alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:x animated:YES];
}
-(void)logout:(id)sender{
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
[lm release];
//[uploadGPS_timer release];
[self.navigationController popViewControllerAnimated:NO];
/*NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.prestocab.com/driver/ajax/logout.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];*/
}
-(IBAction)switchSwitched:(id)sender{
if(onOffline.on){
[label1 setText:#"For Hire"];
[label1 setTextColor:[UIColor colorWithRed:0.0 green:0.8 blue:0.0 alpha:1.0]];
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[self uploadGPS_tick:nil];
}else{
[label1 setText:#"Engaged"];
[label1 setTextColor:[UIColor colorWithRed:0.8 green:0.0 blue:0.0 alpha:1.0]];
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
}
}
-(void)uploadGPS_tick:(NSTimer*)timer{
if(!lat || !lng){
//do nothing
}else{
NSString *urlStr=[[NSString alloc] initWithFormat:#"http://www.prestocab.com/driver/ajax/updateCoords.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setPostValue:lat forKey:#"lat"];
[request setPostValue:lng forKey:#"lng"];
NSLog(#"EOH: %#",lat);
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *response=[request responseString];
NSLog(#"%#",response);
//do nothing
}];
[request setFailedBlock:^{
//NSError *error =[request error];
//do nothing
}];
[request startAsynchronous];
}
}
-(void)locationManager:(CLLocationManager*) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation*) oldLocation{
lat=[[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.latitude];
lng=[[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.longitude];
NSLog(#"%#,%#",lat,lng);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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
{
self.navigationItem.title=#"PrestoCab";
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStylePlain target:self action:#selector(logout:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
//GPS
lm=[[CLLocationManager alloc] init];
if([CLLocationManager locationServicesEnabled]){
lm.delegate=self;
lm.desiredAccuracy=kCLLocationAccuracyBest;
lm.distanceFilter=30.0f;
[lm startUpdatingLocation];
}
//[self check4messages_tick:nil]; //want to start immediately, not in 10/40 seconds' time
[self uploadGPS_tick:nil];
//check4messages_timer=[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:#selector(check4messages_tick:) userInfo:nil repeats:YES];
uploadGPS_timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(uploadGPS_tick:) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
if([uploadGPS_timer isValid]){
[uploadGPS_timer invalidate];
}
}
-(void)dealloc{
[super dealloc];
[uploadGPS_timer release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Also, here is
MyClassViewController.h:
#import <UIKit/UIKit.h>
#interface MyClassViewController : UINavigationController{
IBOutlet UIButton *passenger;
IBOutlet UIButton *driver;
}
#property (nonatomic,retain) UIButton *passenger;
#property (nonatomic,retain) UIButton *driver;
-(IBAction) passengerClicked:(id)sender;
-(IBAction) driverClicked:(id)sender;
#end
and MyClassViewController.m:
#import "MyClassViewController.h"
#import "MainMenuDriver.h"
#implementation MyClassViewController
#synthesize passenger;
#synthesize driver;
-(IBAction)passengerClicked:(id)sender{
NSLog(#"passenger");
}
-(IBAction)driverClicked:(id)sender{
NSLog(#"driver");
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I can see a couple of things:
Your nib file's name is different from the name of your class. If you put nil, the UIViewController will try to load a nib file with the UIViewController's name. Your UIViewController can be MainMenuDriver but your nib file name can be MainMenuDriverNibFileName.nib
[self navigationController] is nil.
For this, do the following:
NSLog(#"hi");
if([self navigationController]){
MainMenuDriver *mainMenuDriver= [[MainMenuDriver alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:mainMenuDriver animated:YES];
}
else{
NSLog(#"Houston we have a problem");
}
Update 1:
So, its nil, what you can do is the following:
Quick and Dirty:
[self presentViewController:mainMenuDriver animated:YES];
Instead of UIViewController switch to a UINavigationController.

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

UIPickerview AppDelegate problem with subviews

Issue with subviews but it may be related to the AppDelegate with respect to placing a UIPickerView in myView as apposed to self.view?
Thanks
[self.view addSubview:pickerView]; <-- works ok
//[myView addSubview:pickerView];<-- fails, can't get dial movement or response
AppDelegate.h
#import <UIKit/UIKit.h>
#class PickerViewController;
#interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AppDelegate.m
#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"
#implementation PickerViewAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pvController = [[PickerViewController alloc] initWithNibName:#"PickerView" bundle:[NSBundle mainBundle]];
[window addSubview:pvController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[pvController release];
[window release];
[super dealloc];
}
#end
Controller.h
#import <UIKit/UIKit.h>
#interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
}
#end
Controller.m
#import "PickerViewController.h"
#implementation PickerViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[myView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myView];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
arrayColors = [[NSMutableArray alloc] init];
for (int i=0; i<60; i++) {
[arrayColors addObject:[NSString stringWithFormat:#"%d", i]];
}
//[self.view addSubview:pickerView]; // <-- works
[myView addSubview:pickerView]; // <-- this fails.. this is what I need for multiple views
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[arrayColors release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
#end
If you comment
[myView addSubview:pickerView];
you will understand, that myView frame is too small. You will be able to interact with the picker only touching that parent view frame. If you want to make the pickers behave normally, you must set the parent view frame to contain the picker frame - make it equal or larger.
Please remember iOs Human Interface Guidelines,
The overall size of a picker, including its background, is fixed at the same size as the keyboard on iPhone.
Make sure that myView has userInteractionEnabled set to YES?