Here is my code so far I can get a URL to work but I want to load a nib from this Alert tab
- (IBAction)aboutAction { // The action called when the about button is clicked.
UIAlertView * aboutView = [[UIAlertView alloc] initWithTitle:#"Alert:" // Create a new UIAlertView named aboutScreen, and allocate it. Set the title to "About"
message:#"MESSAGE GOES HERE"
delegate:self
cancelButtonTitle:#"Got It Thanks!"
otherButtonTitles:#"Donate Now", nil];
[aboutView show]; // Show the UIAlertView on the screen.
[aboutView release]; // Release the UIAlertView from the memory.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) { //Not sure what to do here.
}
}
-(void) alertView:(UIAlertView *) alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 1: {
Accidenthelpercall *help = [[Accidenthelpercall alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:help animated:NO];
}
case 2: {
Accidentdamagecar *damagecar = [[Accidentdamagecar alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:damagecar animated:NO];
}
}
-(void) alertView:(UIAlertView *) alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 1: {
Accidenthelpercall *help = [[Accidenthelpercall alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:help animated:NO];
[help release];
}
case 2: {
Accidentdamagecar *damagecar = [[Accidentdamagecar alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:damagecar animated:NO];
[damagecar release];
}
}
Related
I have 2 alerts I want to present in different cases, I wrote a general function to init the alerts in the beginning and change the messages later, but when I am trying to present the alert I get a crash. When I inspect the notesAlert in runtime it is still nil.
Can someone explain what I did wrong?
#interface viewController (){
UIAlertController *tableAlert;
UIAlertController *notesAlert;
}
#end
#implementation viewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initAlert:tableAlert];
[self initAlert:notesAlert];
}
// func to init the alerts
-(void)initAlert:(UIAlertController*)alert{
alert = [UIAlertController alertControllerWithTitle: #"" message: #"" preferredStyle:UIAlertControllerStyleActionSheet];
[alert setModalPresentationStyle:UIModalPresentationPopover];
[alert.popoverPresentationController setSourceView:self.view];
UIPopoverPresentationController *popover = [alert popoverPresentationController];
CGRect popoverFrame = CGRectMake(0,0, self.view.frame.size.width/2, self.view.frame.size.width/2);
popover.sourceRect = popoverFrame;
UIAlertAction *dismiss = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:dismiss];
}
- (IBAction)showNotes:(id)sender {
// here the notesAlert is still nil
[notesAlert setTitle:#"oops"];
[notesAlert setMessage:#"you pressed the wrong one"];
[self presentViewController:notesAlert animated:YES completion:nil];
}
#end
[self initAlert: notesAlert]; doesn't create notesAlert. Instead, you could use notesAlert = [self initAlert];
Maybe something like this:
#interface ViewController () {
UIAlertController *tableAlert;
UIAlertController *notesAlert;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableAlert = [self initAlert];
self.notesAlert = [self initAlert];
}
// func to init the alerts
- (UIAlertController *) initAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle: #"" message: #"" preferredStyle: UIAlertControllerStyleActionSheet];
[alert setModalPresentationStyle:UIModalPresentationPopover];
[alert.popoverPresentationController setSourceView: self.view];
UIPopoverPresentationController *popover = [alert popoverPresentationController];
CGRect popoverFrame = CGRectMake(0,0, self.view.frame.size.width/2, self.view.frame.size.width/2);
popover.sourceRect = popoverFrame;
UIAlertAction *dismiss = [UIAlertAction actionWithTitle: #"Ok" style: UIAlertActionStyleDefault handler:nil];
[alert addAction: dismiss];
return alert;
}
I've noticed while working with Objective-C, the compiler throws error symbols enforcing the use of either self or an underscore when using a property which I think doesn't happen as harshly when using Swift. I'm now at the crossroads where I believe my view controller isn't being deallocated from the navigation stack. I've used self pretty heavily in order to silence the error symbols but I don't know if this has created retain cycles or not?
#import "MenuController.h"
#import "AppDelegate.h"
#import "CMUser.h"
#import "NotificationsSettingsController.h"
#import "PersonalInfoController.h"
#import "BeLive-Swift.h"
#import "LoginAndSecurityController.h"
#import "SplashController.h"
#define kCellSeparatorTag 100
#implementation SideMenuCell
- (void)layoutSubviews{
[super layoutSubviews];
}
#end
#interface MenuController ()<UITableViewDataSource ,UITableViewDelegate>
#property (nonatomic, strong) NSArray *menuTitles;
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UILabel *usersNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *usersEmailLabel;
#property (weak, nonatomic) IBOutlet UILabel *addPhotoLabel;
#property (weak, nonatomic) IBOutlet SettingsHeaderViewWithPhoto *settingsHeaderViewWithPhoto;
#end
#implementation MenuController
NSString *userFirstNameString;
NSString *userLastNameString;
NSString *userEmailString;
NSMutableString * usersFullNameString;
+ (instancetype)controller{
static MenuController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = ControllerFromMainStoryBoard([self description]);
});
return sharedInstance;
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.settingsHeaderViewWithPhoto.backButton addTarget:self action:#selector(popViewCon:) forControlEvents:UIControlEventTouchUpInside];
[self.settingsHeaderViewWithPhoto.rightButton addTarget:self action:#selector(logoutTapped:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)showUserInfoInHeaderView{
if (userEmailString.length != 0) {
self.settingsHeaderViewWithPhoto.emailSubLabel.text = userEmailString;
} else {
self.settingsHeaderViewWithPhoto.emailSubLabel.text = #"Email not available";
}
if (usersFullNameString.length != 0) {
self.settingsHeaderViewWithPhoto.nameLabel.text = usersFullNameString;
} else {
self.settingsHeaderViewWithPhoto.nameLabel.text = #"Full name not available";
}
[self setMenuArrayBasedOnUserType];
[self.tableView reloadData];
}
-(void)setMenuArrayBasedOnUserType{
if (CMUser.currentUser.type == UserTypeArtist) {
self.menuTitles = #[#[#"Notifications", #"Stripe Info", #"Personal Info", #"Login and Security", #"Invite a Friend", #"Help", #"Legal Agreement"]];
[self.tableView reloadData];
} else if (CMUser.currentUser.type == UserTypeViewer){
self.menuTitles = #[#[#"Notifications", #"Personal Info", #"Login and Security", #"Invite a Friend", #"Help", #"Legal Agreement"]];
[self.tableView reloadData];
} else {
[self showLoggedInAlert];
};
}
-(void)showLoggedInAlert{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Session Time Out"
message:#"You must be logged in first."
preferredStyle:UIAlertControllerStyleAlert]; // 1
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(#"You pressed ok");
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SplashController *vc = [sb instantiateViewControllerWithIdentifier:#"SplashController"];
[self presentViewController:vc animated:YES completion:nil];
}];
[alert addAction:firstAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)getLoggedInUserInfo{
[NetworkManager callEndPoint:USER_DETAILS withDict:nil method:#"GET" JSON:YES success:^(id responseObject) {
userFirstNameString = responseObject[#"data"][0][#"firstname"];
userLastNameString = responseObject[#"data"][0][#"lastname"];
userEmailString = responseObject[#"data"][0][#"emailAddress"];
usersFullNameString = [NSMutableString stringWithString:userFirstNameString];
[usersFullNameString appendString:#" "];
[usersFullNameString appendString: userLastNameString];
[self showUserInfoInHeaderView];
} failure:^(id responseObject, NSError *error) {
NSLog(#"callEndPoint error is: %#", error);
}];
}
-(void)showProfilPhoto{
if ([CMUser currentUser][#"imageUrl"] != NULL) {
self.settingsHeaderViewWithPhoto.addPhotoLabel.text = #"";
[self.settingsHeaderViewWithPhoto.userPhotoButton sd_setImageWithURL:[NSURL URLWithString:[CMUser currentUser][#"imageUrl"]]forState:UIControlStateNormal];
} else {
self.settingsHeaderViewWithPhoto.addPhotoLabel.text = #"Add Photo";
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBarHidden = YES;
[self.tableView registerClass:[SettingsTableViewCell class] forCellReuseIdentifier:#"MenuCell"];
self.tableView.backgroundColor = [UIColor colorWithHex:0x222222];
[self getLoggedInUserInfo];
[self showProfilPhoto];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
CGRect frame = self.view.frame;
frame.size.width = revealWidth;
self.view.frame = frame;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.menuTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray *menuItems = self.menuTitles[section];
return menuItems.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *menuItems = self.menuTitles[indexPath.section];
SettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MenuCell"
forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = menuItems[indexPath.row];
UIView *separatorView = [cell.contentView viewWithTag:kCellSeparatorTag];
if (!separatorView) {
CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
separatorView = [[UIView alloc] initWithFrame:CGRectMake(10, 43, revealWidth - 20, 1)];
separatorView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:separatorView];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath
animated:YES];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
[self notificationsTapped];
default:
break;
}
switch (indexPath.row) {
case 1:
//TODO: IF userrole == artist then show stripe info else if userrole == , show person information
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show strip credit info");
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show personal info");
[self personalInfoTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 2:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show strip personal info");
[self personalInfoTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show login and security");
[self loginSecurityTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 3:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show login and security");
[self loginSecurityTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show Invite a friend");
[self inviteFriendTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 4:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show Invite a friend");
[self inviteFriendTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show Help");
//goto: webURL www.belive.com/help
[self helpTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 5:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show help");
//goto: webURL www.belive.com/help
[self helpTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show legal agreement");
//goto: webURL www.belive.com/legal
[self legalAgreementTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 6:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show legal agreement");
//goto: webURL www.belive.com/legal
[self legalAgreementTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"do nothing because there is not a value for this case.");
}
break;
default:
break;
}
}
}
- (void)notificationsTapped{
NotificationsSettingsController *vc = [NotificationsSettingsController controller];
[self.navigationController pushViewController:vc
animated:YES];
}
- (void)popViewCon: (UIButton*)sender{
[ApplicationDelegate toggleMenu];
}
- (void)logoutTapped: (UIButton*)sender{
[ApplicationDelegate toggleMenu];
[CMUser logOut];
[ApplicationDelegate setController];
}
-(void)personalInfoTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
PersonalInfoController *vc = [sb instantiateViewControllerWithIdentifier:#"PersonalInfoController"];
vc.usersProfilePhoto = self.settingsHeaderViewWithPhoto.userPhotoButton.imageView.image;
[self.navigationController pushViewController:vc animated:YES];
}
-(void)loginSecurityTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
LoginAndSecurityController *vc = [sb instantiateViewControllerWithIdentifier:#"LoginAndSecurityController"];
//LoginAndSecurityController *vc = [LoginAndSecurityController controller];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)inviteFriendTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
InviteFriendController
*vc = [sb instantiateViewControllerWithIdentifier:#"InviteFriendController"];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)helpTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
HelpController *vc = [sb instantiateViewControllerWithIdentifier:#"helpController"];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)legalAgreementTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
LegalAgreementController *vc = [sb instantiateViewControllerWithIdentifier:#"legalController"];
[self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)addPhotoButtonTapped:(UIButton *)sender {
[self configureAddPhotoActionSheet];
}
-(void)configureAddPhotoActionSheet{
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped do nothing.
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Take a Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// take photo button tapped.
[self takePhoto];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Choose Photo from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// choose photo button tapped.
[self selectPhoto];
}]];
[self presentViewController:actionSheet animated:YES completion:nil];
}
- (void)takePhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)selectPhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
//UIImagePickerDelegate Methods
/*
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.profilePictureButton setImage:chosenImage forState:UIControlStateNormal];
self.addPhotoLabel.text = #"";
[self updateProfilePicture:chosenImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
} */
- (void)updateProfilePicture:(UIImage*)image{
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSString *imageString = [NSString stringWithFormat:#"data:image/png;base64,%#", [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
NSDictionary *params = #{#"ImageFileContent" : imageString,
#"ImageName" : #"image.jpg"
};
NSString *uploadEndPoint = #"artist/add-artist-photo";
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.settingsHeaderViewWithPhoto.userPhotoButton animated:YES];
[NetworkManager callEndPoint:uploadEndPoint
withDict:params
method:#"POST"
JSON:YES
success:^(id responseObject) {
NSLog(#"The response object has: %#", responseObject);
[CMUser currentUser][#"imageUrl"] = responseObject[#"data"][0][#"imageURL"];
[[CMUser currentUser] setCurrent];
[HUD hideAnimated:YES];
} failure:^(id responseObject, NSError *error) {
[HUD hideAnimated:YES];
}];
}
#end
This is used to synthesise the variable. It is necessary in objective c and swift works differently then swift. And it doesn't creat any retail cycle.
After doing some reading, I've come to the realization that retain cycles can be created by delegates, using self within a closure, and if other objects are referencing your view controller in question. The problem I had was that this particular view controller is actually the root view controller of a navigation stack in which other view controllers were referencing it as well. Also I made a call to self within two block statements in the code above which also has the potential for creating a strong reference to itself.
want to get input from pickerView in textField when alert show Objective C
NOTE: Opening actionsheet from alertview first dismiss your alertview then open actionsheet.My code may help you regarding opening actionsheet from alertview
#import "ViewController.h"
#interface ViewController ()<UITextFieldDelegate,UIActionSheetDelegate>
{
NSString *alertSelectValue;
UITextField * alertTextField;
}
#end
#implementation ViewController
- (void)viewDidLoad {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello!" message:#"Please enter your name:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.tag=101;
alertTextField.delegate=self;
alertTextField.keyboardType = UIKeyboardTypeNumberPad; alertTextField.placeholder = #"Enter your name";
[alert show];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag==101)
{
[textField setUserInteractionEnabled:YES];
[textField resignFirstResponder];
NSString*String = [NSString stringWithFormat:#"%#",textField.text];
[self openSheet:String];
}
}
-(void)openSheet:(NSString*)text
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:text,#"Test" ,nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
UIViewController *topvc=[self topMostController];
[actionSheet showInView:topvc.view];
}
//-------- Get top Most view to -----
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Index = %d - Title = %#", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
if(buttonIndex == 0)
{
alertTextField.text= [actionSheet buttonTitleAtIndex:buttonIndex];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am writing an iOS app where, in a view there is a button. Clicking on that button gets an action sheet which has options to pick image from camera or gallery. After the image is picked, it should be passed on to another view by manually calling a segue. The image picker appears and the prepare for segue code is executed, but the segue doesnt appear. There are no errors and I have checked all identifiers etc. Here is the code:
-(IBAction)showButtonClicked:(id)sender {
UIActionSheet *photoActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from Library", nil];
photoActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[photoActionSheet showInView:self.tabBarController.tabBar];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
{
[self dismissModalViewControllerAnimated:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
break;
}
case 1:
{
[self dismissModalViewControllerAnimated:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
break;
}
default:
break;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"goToPhotoDetails"]){
FostoPhotoDetailsViewController *photoDetails = (FostoPhotoDetailsViewController *)segue.destinationViewController;
photoDetails.imageView.image = self.buttonImage1;
}
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.buttonImage1 = image;
//FostoPhotoDetailsViewController *photoDetails = [[FostoPhotoDetailsViewController alloc] init];
//photoDetails.imageView.image = image;
[self dismissModalViewControllerAnimated:NO];
NSLog(#"Test");
[self performSegueWithIdentifier:#"goToPhotoDetails" sender:self];
}
Sounds like you are executing a segue with style push without being inside a UINavigationController.
When you call performSegueWithIdentifier:sender: it first correctly calls prepareForSegue:sender: then it tries to retrieve the current navigation controller and push the destination controller doing something like
[self.navigationController pushViewController:destinationController animated:YES];
But since you are not inside a UINavigationController the property navigationController is set to nil, causing the above call to fail silently.
Either change the display style of your segue to something other than push (for instance modal) or embed you controller in a UINavigationController.
I'm trying to reuse code, I have some tutorial view controllers / views, which I would like to call from an action sheet. However, the calling views are different. Sometimes the tutorial view(s) would need to be added as a subview and sometimes they would be added to the navigation controller.
How can I expand my standard function to cater for these two different situations ?
You can see what I'm having to do instead, which means duplicate code :(
I have a class called which holds the standard code, I want to add calls to views here directly.
-(void)showHelpClickButtonAtIndex:(int)buttonIndex:(UIView *)vw {
if (buttonIndex == CommonUIHelpPagesBtnIdx) {
// do nothing
} else if (buttonIndex == 0) {
NSLog(#"Tutorial here");
}
}
I use in one view like this ...
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
CommonUI *cui = [CommonUI alloc];
[cui showHelpClickButtonAtIndex:buttonIndex:self.view];
[cui release];
if (buttonIndex == CommonUIHelpPagesBtnIdx) {
UIViewController *theController = [[HelpViewController alloc]
initWithNibName:#"HelpView"
bundle:nil onPage:HelpPageCalcBalance];
[self.navigationController.topViewController
presentModalViewController:theController animated:YES];
[theController release];
}
}
And is another view like this...
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
[cui showHelpClickButtonAtIndex:buttonIndex:self.view];
if (buttonIndex == CommonUIHelpPagesBtnIdx) {
theController = [[HelpViewController alloc] initWithNibName:#"HelpView"
bundle:nil onPage:HelpPageGettingStarted];
[self.view addSubview:theController.view];
}
}
Maybe the actionSheets could share one same delegate that would be a root viewController or the appDelegate that would know what to do according to it's current state. Hence the same actionSheet method would be used in both case.
If you put your appDelegate which can always be reached as the actionSheetDelegate, you should be able to gain control on every way to present your view, either modally or not in any of your views + the window.
EDIT (re-Edited with your code): Maybe try this
MyAppDelegate.h:
#interface MyAppAppDelegate : NSObject <UIApplicationDelegate, UIActionSheetDelegate>
(...)
- (void) showHelp;
MyAppDelegate.m:
- (void) showHelp {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Tutorial", #"Help Pages", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:window];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == CommonUIHelpPagesBtnIdx) {
UIViewController *theController = [HelpViewController alloc];
if ([[self lvc] superview] != nil) {
theController = [initWithNibName:#"HelpView"
bundle:nil
onPage:HelpPageGettingStarted];
[window addSubview:theController.view];
} else {
theController = [initWithNibName:#"HelpView"
bundle:nil
onPage:HelpPageCalcBalance];
UINavigationController * navController = [tabBarController selectedViewController];
[navController.topViewController presentModalViewController:theController animated:YES];
}
[theController release];
}
}
and in your viewControllers:
- (void)viewDidLoad {
[super viewDidLoad];
MyAppDelegate *delegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:delegate
action:#selector(showHelp)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
self.navigationItem.rightBarButtonItem = infoItem;
[infoItem release];
}
Didn't try it but it should work.