ShareKit: Customizing text for different sharers (SHKActionSheet) - objective-c

According to the official FAQ from ver.2 to customize your text/content depending on what sharer was selected by the user, you need:
subclass from SHKActionSheet and override
dismissWithClickedButtonIndex
set your new subclass name in
configurator (return it in (Class)SHKActionSheetSubclass;).
It doesn't work for me. But even more: I put
NSLog(#"%#", NSStringFromSelector(_cmd));
in (Class)SHKActionSheetSubclass to see if it's even got called. And it's NOT ;(( So ShareKit doesn't care about this config option...
Has anybody worked with this before?
thank you!
UPD1: I put some code here.
Here's how my subclass ITPShareKitActionSheet looks like. According to the docs I need to override dismissWithClickedButtonIndex:animated:, but to track if my class gets called I also override the actionSheetForItem::
+ (ITPShareKitActionSheet *)actionSheetForItem:(SHKItem *)item
{
NSLog(#"%#", NSStringFromSelector(_cmd));
ITPShareKitActionSheet *as = (ITPShareKitActionSheet *)[super actionSheetForItem:item];
return as;
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animate
{
NSLog(#"%#", NSStringFromSelector(_cmd));
NSString *sharersName = [self buttonTitleAtIndex:buttonIndex];
[self changeItemForService:sharersName];
[super dismissWithClickedButtonIndex:buttonIndex animated:animate];
}
And here's what I do in code to create an action sheet when user presses 'Share' button:
- (IBAction)shareButtonPressed:(id)sender
{
// Create the item to share
SHKItem *item = [SHKItem text:#"test share text"];
// Get the ShareKit action sheet
ITPShareKitActionSheet *actionSheet = [ITPShareKitActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showInView:self.view]; // showFromToolbar:self.navigationController.toolbar];
}
When I run this code, press 'Share' button and select any sharer I expect to get two lines in log:
actionSheetForItem: - custom action sheet got created
dismissWithClickedButtonIndex:animated: - custom mechanics to
process action sheet's pressed button got called.
But for some reason I get only the first line logged.

I was having the same issues but I've suddenly got it to call my Subclass successfully.
Firstly My Configurator is setup as so:
-(Class) SHKActionSheetSubclass{
return NSClassFromString(#"TBRSHKActionSheet");
}
Now My Subclass:
.h File
#import "SHKActionSheet.h"
#interface TBRSHKActionSheet : SHKActionSheet
#end
.m implementation override:
#import "TBRSHKActionSheet.h"
#import "SHKActionSheet.h"
#import "SHKShareMenu.h"
#import "SHK.h"
#import "SHKConfiguration.h"
#import "SHKSharer.h"
#import "SHKShareItemDelegate.h"
#implementation TBRSHKActionSheet
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
+ (SHKActionSheet *)actionSheetForItem:(SHKItem *)i
{
NSLog(#"%#", NSStringFromSelector(_cmd));
SHKActionSheet *as = [self actionSheetForType:i.shareType];
as.item = i;
return as;
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
NSInteger numberOfSharers = (NSInteger) [sharers count];
// Sharers
if (buttonIndex >= 0 && buttonIndex < numberOfSharers)
{
bool doShare = YES;
SHKSharer* sharer = [[NSClassFromString([sharers objectAtIndex:buttonIndex]) alloc] init];
[sharer loadItem:item];
if (shareDelegate != nil && [shareDelegate respondsToSelector:#selector(aboutToShareItem:withSharer:)])
{
doShare = [shareDelegate aboutToShareItem:item withSharer:sharer];
}
if(doShare)
[sharer share];
}
// More
else if ([SHKCONFIG(showActionSheetMoreButton) boolValue] && buttonIndex == numberOfSharers)
{
SHKShareMenu *shareMenu = [[SHKCONFIG(SHKShareMenuSubclass) alloc] initWithStyle:UITableViewStyleGrouped];
shareMenu.shareDelegate = shareDelegate;
shareMenu.item = item;
[[SHK currentHelper] showViewController:shareMenu];
}
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
Finally on my implementation file I've not modified the call to SHKActionSheet as Vilem has suggested because of some dependancies that seemed to cause conflicts for me.
So this is my caller (straight from tutorial):
NSURL *url = [NSURL URLWithString:#"http://getsharekit.com"];
SHKItem *item = [SHKItem URL:url title:#"ShareKit is Awesome!" contentType:SHKURLContentTypeWebpage];
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// ShareKit detects top view controller (the one intended to present ShareKit UI) automatically,
// but sometimes it may not find one. To be safe, set it explicitly
[SHK setRootViewController:self];
// Display the action sheet
[actionSheet showFromToolbar:self.navigationController.toolbar];
This Calls no problems for me.

edit: by far the best way to achieve this is to use SHKShareItemDelegate. More info is in ShareKit's FAQ.

Related

CNUI ERROR Selection predicates are set but the delegate does not implement contactPicker:didSelectContact:

I try to use the new iOS 9.0 CNContactPickerViewController to select a contact in objective-C. I set the delegate and implement the CNContactPickerDelegate methods.
#import ContactsUI;
#import Contacts;
//-----------------------------------------------------------------------
- (void) presentContacts
{
CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
contactPicker.predicateForEnablingContact = [NSPredicate predicateWithFormat:#"familyName LIKE[cd] 'smith'"];
contactPicker.predicateForSelectionOfContact = [NSPredicate predicateWithFormat:#"TRUEPREDICATE"];
[_myViewController presentViewController:contactPicker animated:YES completion:nil];
}
//-----------------------------------------------------------------------
- (void) contactPickerDidCancel: (CNContactPickerViewController *) picker
{
NSLog(#"didCancel");
}
//-----------------------------------------------------------------------
- (void) contactPicker: (CNContactPickerViewController *) picker
didSelectContact: (CNContact *) contact
{
NSLog(#"didSelectContact"):
}
//-----------------------------------------------------------------------
- (void) contactPicker: (CNContactPickerViewController *) picker
didSelectContactProperty: (CNContactProperty *) contactProperty
{
NSLog(#"didSelectProperty");
}
//-----------------------------------------------------------------------
The contacts picker is presented with 'smith' selectable but I get the following message:
[CNUI ERROR] Selection predicates are set but the delegate does not implement contactPicker:didSelectContact: and contactPicker:didSelectContactProperty:. Those predicates will be ignored.
And I never get any log from the delegate methods. It behaves exactly as the line
contactPicker.delegate = self;
is ignored. Even I click on the "cancel" button in the picker, I don't get my "didCancel" message but I get another message:
plugin com.apple.MobileAddressBook.ContactsViewService invalidated
I found in https://forums.developer.apple.com/thread/12275 somebody with the similar problem in swift and he solved it telling us: "So I found that the ContactsPicker I was calling was in the wrong module" but I do not understand how we can get the wrong module and how to call the "right" module.
I have the same problem on the simulator and on a real device (iPad).
Thanks to Joel in my related question With CNContactPickerViewController in iOS 9.0, how to enable/disable single or multiple selection?, I found that I just forgot to store the CNContactPickerViewController in a property that exists the time the user make the selection.
So my code becomes:
- (void) presentContacts
{
_contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
...
}

Passing data between view controllers in storyboards using segue

I'm new at iOS development and after reading many tutorials about passing variables and i still need your help.
This function in my PP.m file:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Btn1Transport"])
{
[segue.destinationViewController setMyData:(50)];
NSLog(#"Transporter1");
}
if ([segue.identifier isEqualToString:#"Btn2Transport"])
{
[segue.destinationViewController setMyData:(100)];
NSLog(#"Transporter2");
}
}
This is in my Category.m file:
- (void)viewDidLoad{
[super viewDidLoad];
recipeLabel.text = #"Hello"; //for test, is working
}
-(void)setMyData:(int)myData
{
NSLog(#"Happines %d",myData);
NSString* result = [NSString stringWithFormat:#"%d", myData];
recipeLabel.text = result; //not working
}
The problem is at the line NSLog(#"Happines %d",myData); my data prints just fine, but not gets set to recipeLabel. So for testing if it works i made recipeLabel.text = #"Hello"; and the label is just fine. What am I doing wrong? And sorry for the beginner question.
No, you can't write to the TextLabel directly from the prepareForSegue event, the value will be overwritten when the destination view controller will load ...
You have to set a variable in the destination view controller and then you have to set the value of your label equal to the value of the variable in the viewDidLoad event of the destination view controller to make it works ...
//The variable myIntVar is a private variable that should be declared in the header file as private
- (void)viewDidLoad{
[super viewDidLoad];
recipeLabel.text = myIntVar; //set the value equal to the variable defined in the prepareForSeque
}
-(void)setMyData:(int)myData
{
NSLog(#"Happines %d",myData);
NSString* result = [NSString stringWithFormat:#"%d", myData];
myIntVar = result;
}

Objects string value is not set correctly

I'm writing a small project time management program for myself and have run into a problem which has confounded me.
They way it's set up is that I have an object called TCObject which I use in another object called ProjectTimerController. ProjectTimerController is a NSWindowController and it has it's own NIB file.
What I'm doing is pretty straight forward. When you click a line in a NSTableView ProjectTimerController finds a TCObject which corresponds to that line. It then loads info from that TCObject into an interface where you can view and edit some stuff.
Here's a screenshot of what it looks like:
Now when I change the text in NSTextView and then press the Add button the -saveString function is called and currentSelection (which is a TCObject and represents the currently selected line) and it's notes variable is set. I know that _notes is set as the new value as NSLog function logs the correct string being in _notes when setString is run. The same, correct, string is logged in -tableViewSelectionDidChange: just before currentSelection is set as the newly selected object.
But if I select the line where I just changed the notes it just loads the same text, "Initial String" and checking _notes tells me it's "Initial String".
Thing I don't have this problem with isFinished. When the Finished check box is toggled I set the corresponding TCObjects' isFinished Boolean value to the same value as the checkbox. This the object remembers and correctly changes depending on what line I have selected.
[EDIT]
*I've added a clearer explanation here.
I click a line in the NSTableView (lets say the top one)
This loads a corresponding TCObject from the myProjects array and that object's variable are added to the Notes NSTextView box and Finished is toggled on or off.
If I now write into The Notes box and press "Add" the text there is set into that TCObject's _notes variable.
So If I click another line some other text is loaded into the Notes box. Clicking back on the top line should give me the string I just wrote into Notes in step 3. But it doesn't. _notes always seems to contain the string I set when I initialize it in the -init method.
The "Finished" checkbox works fine. When I click that the state is saved and loaded correctly when I click a line.
I know that _notes is correctly set when I press the Add button as the NSLog method in setString logs the string I have written into Notes when I press the Add button.
[/EDIT]
Here below is a barebones version of TCObject and ProjectTimerController.
//TCObject.h
#interface TCObject : NSObject
{
NSString *_notes;
Boolean _isFinished;
}
#property (retain, nonatomic) NSString *notes;
#property (nonatomic) Boolean isFinished;
#end
//TCObject.m
#import "TCObject.h"
#implementation TCObject
#synthesize notes = _notes, isFinished = _isFinished;
-(id)init{
if (self = [super init]) {
self.notes = #"Initial string";
self.isFinished = NO;
}
return self;
}
- (void)dealloc {
[_notes release]; _notes = nil;
[super dealloc];
}
-(void)setNotes:(NSString *)notes {
[notes retain];
[_notes release];
_notes = notes;
NSLog(#"Setting _notes as: %#", _notes);
}
-(NSString *)notes {
NSLog(#"Getting _notes, which is: %#", _notes);
return _notes;
}
#end
//ProjectTimerController.m
- (id)initWithWindow:(NSWindow *)window {
self = [super initWithWindow:window];
if (self)
{
myProjects = [[NSMutableArray alloc]init];
currentSelection = nil;
TCObject *newProject = [[TCObject alloc] init];
TCObject *newProject2 = [[TCObject alloc] init];
TCObject *newProject3 = [[TCObject alloc] init];
TCObject *newProject4 = [[TCObject alloc] init];
[myProjects addObject:newProject];
[myProjects addObject:newProject2];
[myProjects addObject:newProject3];
[myProjects addObject:newProject4];
}
return self;
}
-(IBAction)isFinishedToggle:(id)sender {
if(currentSelection != nil){
currentSelection.isFinished = finishedCheckBtn.state;
}
}
-(IBAction)saveString:(id)sender {
if(currentSelection != nil){
currentSelection.notes = [[notesField textStorage] string];
}
}
//delegate function for NSTableView
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
NSInteger selectedIndex = [table selectedRow];
if(selectedIndex == -1){
return;
}
//here the correct notes string is printed
NSLog(#"curr: %i", currentSelection.notes);
currentSelection = [myProjects objectAtIndex:selectedIndex];
NSString *notesInfo = currentSelection.notes;
Boolean isFinishedInfo = currentSelection.isFinished;
[notesField setString:notesInfo];
[finishedCheckBtn setState:isFinishedInfo];
}
Finally found the problem. Seems that changing notes in this way:
-(IBAction)saveString:(id)sender {
if(currentSelection != nil){
currentSelection.notes = [[notesField textStorage] string];
}
}
causes some problems. Everything works fine if I do it this way:
-(IBAction)saveString:(id)sender{
if(currentSelection != nil){
NSString *temps = [NSString stringWithFormat:#"%#", [[notesField textStorage] string]];
currentSelection.notes = temps;
}
}
So I'm guessing what was going on is that _notes was pointing to the text contained in my NSTextView. So when I changed the text there _notes also changed or something like that...

Suppressing the text completion dropdown for an NSTextField

I'm trying to create the effect of an NSComboBox with completes == YES, no button, and numberOfVisibleItems == 0 (for an example, try filling in an Album or Artist in iTunes's Get Info window).
To accomplish this, I'm using an NSTextField control, which autocompletes on -controlTextDidChange: to call -[NSTextField complete:], which triggers the delegate method:
- (NSArray *)control:(NSControl *)control
textView:(NSTextView *)textView
completions:(NSArray *)words
forPartialWordRange:(NSRange)charRange
indexOfSelectedItem:(NSInteger *)index;
I've gotten this working correctly, the only problem being the side effect of a dropdown showing. I would like to suppress it, but I haven't seen a way to do this. I've scoured the documentation, Internet, and Stack Overflow, with no success.
I'd prefer a delegate method, but I'm open to subclassing, if that's the only way. I'm targeting Lion, in case it helps, so solutions don't need to be backward compatible.
To solve this, I had to think outside the box a little. Instead of using the built-in autocomplete mechanism, I built my own. This wasn't as tough as I had originally assumed it would be. My -controlTextDidChange: looks like so:
- (void)controlTextDidChange:(NSNotification *)note {
// Without using the isAutoCompleting flag, a loop would result, and the
// behavior gets unpredictable
if (!isAutoCompleting) {
isAutoCompleting = YES;
// Don't complete on a delete
if (userDeleted) {
userDeleted = NO;
} else {
NSTextField *control = [note object];
NSString *fieldName = [self fieldNameForTag:[control tag]];
NSTextView *textView = [[note userInfo] objectForKey:#"NSFieldEditor"];
NSString *typedText = [[textView.string copy] autorelease];
NSArray *completions = [self comboBoxValuesForField:fieldName
andPrefix:typedText];
if (completions.count >= 1) {
NSString *completion = [completions objectAtIndex:0];
NSRange difference = NSMakeRange(
typedText.length,
completion.length - typedText.length);
textView.string = completion;
[textView setSelectedRange:difference
affinity:NSSelectionAffinityUpstream
stillSelecting:NO];
}
}
isAutoCompleting = NO;
}
}
And then I implemented another delegate method I wasn't previously aware of (the missing piece of the puzzle, so to speak).
- (BOOL)control:(NSControl *)control
textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
// Detect if the user deleted text
if (commandSelector == #selector(deleteBackward:)
|| commandSelector == #selector(deleteForward:)) {
userDeleted = YES;
}
return NO;
}
Update: Simplified and corrected solution
It now doesn't track the last string the user entered, instead detecting when the user deleted. This solves the problem in a direct, rather than roundabout, manner.

passing button id from one .m to another .m

Need to pass a button id from one .m to another and clear my table view at the same time so i can load new data in. Nothing seems to be happening, the table still contains what it was populated with on viewDidLoad and the background image does not change. Please help.
There are no warning or errors so im not sure what im doing wrong but the value of senderIdentifier in ClassTwo.h is always 0
Thank you.
in my classOne.m i have the following button action
-(IBAction) btnPushViewtabel:(id)sender{
ClassTwo *classtwo = [[ClassTwo alloc]init];
classtwo.myTable = nil;
classtwo.senderIdentifier = [sender tag];
[self.view addSubview:viewtableController.view];
}
in ClassTwo.h
#interface ClassTwo : UIViewController <UITableViewDelegate, UITableViewDataSource> {
....
int senderIdentifier;
}
#property (nonatomic, readwrite) int senderIdentifier;
in ClassTwo.m
if(senderIdentifier == 0){
// getNewData
//set background img1
} else {
// getNewData
//set background img2
}
if(mySenderIdentifier == 0){ //
getNewData //set background img1 }
else { // getNewData //set
background img2 }
what method is this code block in?
Edit:
This Line:
classtwo.mySenderIdentifier = [sender
tag];
is called after:
viewDidLoad
In other words- when you are checking it you havn't actually set it yet.
There are a number of ways to solve this but my personal preference would be to create a custom init method for classTwo like this:
-(id)initWithSenderIdentifier:(int)identifier{
if(self=[super init]){
self.senderIdentifier = identifier;
self.myTable = nil;
}
return self;
}
Or elimate the need to store the int (given that's only used to set the background once)
-(id)initWithSenderIdentifier:(int)identifier{
if(self=[super init]){
if(identifier)//set backgroundimg1;
else //set backgroundimg2;
self.myTable = nil;
}
return self;
}
you use it like this:
ClassTwo *classtwo = [[ClassTwo alloc]
initWithSenderIdentifier:[sender tag]];
If this is helpful, please mark it was answered