Is a custom UIActivity with the bookmark icon possible? - ios

Part of my iOS app is a file viewer with a UIActivityViewController. From it, I would like users to be able to do normal activity view stuff with the file, like mailing it, saving it to photos (if it's a photo), and everything else. However, my app has a bookmarks feature. I would like for users to be able to bookmark files from this menu. I have created a custom UIActivity to add the object to the bookmarks list, but I have not been able to figure out how to have it use the system bookmark icon. Is this even possible?
EDIT: For clarification, this is in the menu that you get when you click a "share" button.

Easy peasy. It all depends on how you are initiating it, is it in a navigation controller, swipe gesture, toolbar item or just a regular button? Either way it doesn't matter but if you want a specific answer you'll have to ask a specific question.
Here is an example how to accomplish your goal buy using a bookmark button that is connected in Storyboard interface, however the IBActions for addToFav and download aren't an interface and don't need to be:
-(IBAction) moreActions:(id)sender {
UIActionSheet *moreActions = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Add To Favorites", #"Download For Offline View",#"Open Document In :", nil];
[moreActions showInView:self.view];
}
And the way you perform actions is by calling the method below:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: [self addToFav:self]; //See IBAction below
break;
case 1: [self download:self]; //See IBAction below
break;
case 2: {
//UIDocumentInteractionController:
NSURL *docURL = [NSURL URLWithString:#"filePathStringHere"];
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:docURL];
[documentInteractionController setDelegate:self];
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
break;
}
}
- (IBAction)download:(id)sender {
//enter your download code here
}
- (IBAction)addToFav:(id)sender {
//Code to add to your favorites
}

Related

How to add a extra button on Alert view using xcode?

EXTRA INFO: I must have asked wrong, I'd like the user to click the button and be redirected to Facebook!
I need to add a visit facebook button called "facebook" as swell in the code below! Right now I just have an ok button.
Also if possible could you help me understand, how I will be able to retrieve user information - e.g I'll ask them to add there email in the textfield, when they press ok then where will I store the email and obtain it, how does that work, and what do I need to read to find out?
Try to add your buttons in otherButtonTitles
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Refresh"
message:#"Are you want to Refresh Data"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Button1",#"Button2",#"Button3",
nil];
As per Duncan's answer you can use delegate and get which button is clicked.so on specific button's tap you can redirect to facebook page using below code.
UIAlertView *info = [[UIAlertView alloc]initWithTitle:#"Yup" message:#"You've won! Like Us on Facebook too" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Facebook", nil];
info.tag = 10;
[info show];
So when the user presses the Facebook button the delegate method alertView:clickedButtonAtIndex will be called so at that time check alert.tag and then check if facebook button is tapped then show another alert.
Your Delegate Method should be
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 10)
{
switch (buttonIndex) {
case 0:
NSLog(#"Cancel");
break;
case 1:
NSLog(#"Facebook");
[self showAlertForRedirect];
break;
default:
break;
}
}
else if (alertView.tag == 20)
{
switch (buttonIndex) {
case 0:
NSLog(#"Cancel");
break;
case 1:
NSLog(#"OK");
[self RedirectNow];
break;
default:
break;
}
}
}
-(void)showAlertForRedirect
{
UIAlertView *info2 = [[UIAlertView alloc]initWithTitle:#"Note" message:#"Would you like to redirect on facebook?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
info2.tag = 20;
[info2 show];
}
-(void)RedirectNow
{
NSURL *fanPageURL = [NSURL URLWithString:#"fb://profile/yourid"];
if (![[UIApplication sharedApplication] openURL: fanPageURL])
{
NSURL *webURL = [NSURL URLWithString:#"https://www.facebook.com/yourpagename"];
[[UIApplication sharedApplication] openURL: webURL];
}
}
As the other poster said, you can add additional buttons by providing button titles in the otherButtonTitles parameter to the initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: method.
You can set up your alert to have an input field by setting the alertViewStyle property of the button returned by the above method to UIAlertViewStylePlainTextInput
You need to pass in self (your view controller) as the delegate parameter.
Then you need to implement the method alertView:didDismissWithButtonIndex:
In that method, you will get the button index the user selected. You can use the textFieldAtIndex: method to get a pointer to the text field, and get the user-entered text.

send action from button in collection view

I have a query that pulls the 10 nearest objects and displays the title of each in a button in a collection view. What I am trying to do is have an action that opens a new view controller displaying information of the object chosen from the collection view. What I need help with is connecting the chosen object in the buttons action. I am new to Xcode and could use some advice. This is the code defining the query:
int i = 0;
for (PFObject *object in objects) {
if (i >= [self.EventTitles count]) break;//to make sure we only write up to the max number of UILabels available in EventTitles
[(UIButton *)self.EventButtons[i] setTitle:[object objectForKey:#"name"] forState:UIControlStateNormal];
[(UIButton *)self.EventButtons[i] addTarget:self action:(social:) forControlEvents:UIControlEventTouchUpInside];
i++;
}
And then there is the code with the action
- (IBAction)social:(id)sender{
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Close" destructiveButtonTitle:nil otherButtonTitles:#"Call",#"Directions",#"Website",#"Checkin", nil];
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (buttonIndex == 0) {
NSString* yourActualNumber = [NSString stringWithFormat:#"tel:%#",????????];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]];
NSLog(#"phone");
//Check Twitter accessibility and at least one account is setup.
}
I just do not know how to connect the object to the action, and then display that information on the new view controller.
UPDATE: I updated the code with the new line in the query, however it fails stating that the action social is undeclared however it is? I also changed the action to perform an action sheet rather than a new view controller. What I need now it a connection from the object to the action and how to write in the object for key appropriately in the action sheet function.
What I need help with is connecting the chosen object in the buttons
action.
[button addTarget:self action:#selector(DetailEvent1:) forControlEvents:UIControlEventTouchUpInside]
and then display that information on the new view controller.
I don't really know what you are asking here, if you clarify I'll try my best to help :)
Here is an example where an action sheet is displayed when an item is selected. I'm simply using the index path of the selected item, but I assume you have some other data you'd need to save so the Action Sheet delegate can use it.
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:#"Call number %d", indexPath.row];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id < UIActionSheetDelegate >)self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:str, nil];
[actionSheet showInView:self.view];
}

iOS and xcode: how to create a modal segue to a special view only the first time the app is used

I want to make a "terms and conditions" screen that pops up the very first time that app is opened. On this view I would add a button that says "agree," and upon clicking it the code would execute:
[self dismissViewControllerAnimated:YES completion:nil];
...and would go to the first view of the app.
I am currently using a Tab Bar Controller that has 4 ViewControllers. So basically, I just need to have some method in viewWillAppear on my first ViewController that checks for an NSUserDefault key:value. The first time the app opens, it will be zero. After they click agree, I'll set it to 1 and the bit of code would never execute again.
Can you please offer some code to accomplish the task of routing the view from the firstViewController's view to this alternate view controller upon loading the app?
Thanks!
In the viewWillAppear method in your FirstViewController, check NSUserDefaults then present your TermsViewController. After user click agree in TermsViewController, set NSUserDefaults then call
[self.presentingViewController dismissModalViewControllerAnimated:YES]
The use of the popover window can get complicated. Try something like the following if you have little experience with Objective-C.
- (void)viewDidLoad {
if ([termsvalue == 0]) {
NSString *msg = [NSString stringWithFormat:#"Do you agree with the terms of use?"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Confirmation -"
message:msg
delegate:self
cancelButtonTitle:#"Disagree"
otherButtonTitles:#"I agree", nil];
[alert setTag:100];
[alert show];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // Go
if ([alertView tag] == 100) {
if (buttonIndex == 1) {
// The user has agreed
}
}
}

Calling an IBaction in a alertView if statement

Me and my buddy are working on an app, we're total newbies but have come a long way with books and goggling.
We're stuck now on this thing. We have a bunch of texfields that we have clear button linked to it with this action, but then we want that action to be called if you click "Yes" on one of the alert view buttons.
- (IBAction)clearText:(id)sender {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
We also have this alert view:
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Warning"
message: #"Do you want to delete?"
delegate: self
cancelButtonTitle: #"No"
otherButtonTitles: #"Yes", nil];
- (void)alertView: (UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"No"]) {
}
else if ([buttonTitle isEqualToString:#"Yes"]){
Spelare1Slag1.text = #"";
}
}
So this is how we think we should do it, but we don't know what to put in the else if statement. We want the textfields to clear when you press the "yes" button in the alert view, and not when you press "no"
Thanks in advance!
The clearText method, I'm assuming, is a custom method you created to delete the text in both the fields right? So instead of it being an IBAction, it should be a void method :
- (void)clearText {
Spelare1Slag1.text = #"";
Spelare1Slag2.text = #"";
}
Now all you need to do in your UIAlertView delegate method, is call the clearText method :
- (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Yes"]){
[self clearText];
}
}
Hope this helps
You could dismiss the AlertView in case of user has clicked NO If I understand question properly.
You could dismiss the alertview like this
[alertView dismissWithClickedButtonIndex:0 animated:YES];
but make sure to see if NO has index 0 or 1, if you are not sure then just do like this
[alertView dismissWithClickedButtonIndex:nil animated:YES];
Methods of type IBAction are just like any other method, and you can call them in your code directly. In fact, IBAction is simply a macro evaluating to void. Usually, you'll pass a nil sender argument when it needs to be called outside of the context of a target / action event being triggered.
[self clearText:nil];
Why do you need to check the actual button text? Checking the button index is the most efficient way to go. If you have two fields in your alertView, just check if the index is 0 or 1 and your good to go. Your doing extra work checking the actual text.
And btw, just do a check for the index of the YES button if you don't need to do anything specific when they press no. (Don't check both indexes if you don't need to).

iPhone Action Sheet Cancel Button Only Responds to Touch Near Center

I have encountered something like this before, where a button only responds to touches in part of its view. I found a image view with a transparent area was on a higher z-level, which obscured the touches where the overlap occurred. This was due to the button being added as a subview before the obscuring view was added.
In the case of an action sheet, I thought it would be at the highest z-level over anything else. Since the action sheet was just initialized and shown, there is no way that I can see that something is covering the button.
(For what it's worth, I just converted the app into a universal app, and am testing in both iOS 4.3 and 5.0. There is no cancel button in the action sheet on the iPad. This problem exists when simulating for iPhone for both iOS 4.3 and 5.0.)
I'm looking for other ideas as to what is causing the problem.
UPDATE
Here is the code to show the action sheet
- (void) shareButtonPressed {
SHKItem *item = [SHKItem larkspreeSearchResult:searchResult];
item.image = eventImageRaw;
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showInView:self.view];
}
Since this is for a subclassed action sheet, this, right out of ShareKit, might be helpful, too.
+ (SHKActionSheet *)actionSheetForType:(SHKShareType)type
{
SHKActionSheet *as = [[SHKActionSheet alloc] initWithTitle:SHKLocalizedString(#"Share")
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
as.item = [[[SHKItem alloc] init] autorelease];
as.item.shareType = type;
as.sharers = [NSMutableArray arrayWithCapacity:0];
NSArray *favoriteSharers = [SHK favoriteSharersForType:type];
// Add buttons for each favorite sharer
id class;
for(NSString *sharerId in favoriteSharers)
{
class = NSClassFromString(sharerId);
if ([class canShare])
{
[as addButtonWithTitle: [class sharerTitle] ];
[as.sharers addObject:sharerId];
}
}
// Add More button
[as addButtonWithTitle:SHKLocalizedString(#"More...")];
// Add Cancel button
[as addButtonWithTitle:SHKLocalizedString(#"Cancel")];
as.cancelButtonIndex = as.numberOfButtons -1;
return [as autorelease];
}
The showInView method has not been overridden.
If this view controller is part of a UINavigationController with a toolbar configured or a UITabBarController then any action sheet needs to be presented in the parent controller's view. For instance, [actionSheet showInView:self.navigationController.view]; or [actionSheet showInView:self.tabBarController.view];.

Resources