Blue highlighting not appear with custom UITextView - ios

I created a subclass of UITextView to add a custom UIMenuItem. The problem is that when I press my custom action to display custom item, the text is not highlighted. Any idea?
ActionsTextView.h
#import <UIKit/UIKit.h>
#protocol ActionsDelegate <NSObject>
- (void)addDreamSignalWithText:(NSString *)text range:(NSRange)range;
#end
#interface ActionsTextView : UITextView
#pragma mark - Delegate
#property IBOutlet id<ActionsDelegate>actionsDelegate;
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender;
#end
ActionsTextView.m
#import "ActionsTextView.h"
#implementation ActionsTextView
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(addDreamSignalAction:)) {
return YES;
}
return NO;
}
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender {
if ([_actionsDelegate respondsToSelector:#selector(addDreamSignalWithText:range:)]) {
[_actionsDelegate addDreamSignalWithText:[self.text substringWithRange:self.selectedRange]
range:self.selectedRange];
}
// Deselect text
self.selectedTextRange = nil;
}
#end
Thanks!!

Thanks to rmaddy, I found the solution.
Here is the code:
ActionsTextView.h
#import <UIKit/UIKit.h>
#protocol ActionsDelegate <NSObject>
- (void)addDreamSignalWithText:(NSString *)text range:(NSRange)range;
#end
#interface ActionsTextView : UITextView
#pragma mark - Delegate
#property IBOutlet id<ActionsDelegate>actionsDelegate;
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender;
#pragma mark - Notifications
- (void)menuControllerWillShow:(NSNotification *)notification;
#end
ActionsTextView.m
#import "ActionsTextView.h"
#implementation ActionsTextView
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(addDreamSignalAction:)) {
return YES;
}
return NO;
}
#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender {
if ([_actionsDelegate respondsToSelector:#selector(addDreamSignalWithText:range:)]) {
[_actionsDelegate addDreamSignalWithText:[self.text substringWithRange:self.selectedRange]
range:self.selectedRange];
}
// Deselect text
self.selectedTextRange = nil;
}
#pragma mark - Notifications
- (void)menuControllerWillShow:(NSNotification *)notification {
if (self.selectedRange.length == 0) {
[self select:self];
}
}
#end

Related

Not getting chat bubble using JSQMessageViewController in ios

I am not getting Chat Bubble in JSQMessageViewController but not able to get what to method to be add to get chat bubble in my chat page in IOS I am getting only Textbox and send button when I type text and click send not getting bubbles I am new please help me.
// .h File content
// ChatpageViewController.h
// ChatApp
#import <UIKit/UIKit.h>
#import <JSQMessagesViewController/JSQMessages.h>
#import <JSQMessagesViewController.h>
#import "JSQMessagesCollectionViewFlowLayout.h"
#import "JSQMessages.h"
#import "JSQPhotoMediaItem.h"
#import "JSQLocationMediaItem.h"
#import "JSQVideoMediaItem.h"
#import "JSQMessagesMediaViewBubbleImageMasker.h"
#import "JSQMessagesAvatarImage.h"
#import "JSQMessagesAvatarImageFactory.h"
#import "JSQMessagesBubbleImage.h"
#import "JSQMessagesBubbleImageFactory.h"
#import "UIImage+JSQMessages.h"
#interface ChatpageViewController : JSQMessagesViewController<JSQMessagesCollectionViewDataSource,JSQMessagesCollectionViewDelegateFlowLayout,JSQMessagesCollectionViewCellDelegate,JSQMessageData,JSQMessageMediaData,JSQMessageAvatarImageDataSource,JSQMessageBubbleImageDataSource>
#property(nonatomic,strong)NSDictionary * receivedict;
#property (strong, nonatomic) IBOutlet UILabel *name;
#property (strong, nonatomic) IBOutlet UILabel *mobile;
- (IBAction)cancelbtn:(id)sender;
#end
M File starts from here
//
// ChatpageViewController.m
// ChatApp
//
#import "ChatpageViewController.h"
#interface ChatpageViewController ()
{
}
#end
#implementation ChatpageViewController
#synthesize receivedict,name,mobile;
-(void)viewWillAppear:(BOOL)animated
{
self.collectionView.collectionViewLayout.springinessEnabled = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES animated:YES];
NSLog(#"%#",receivedict);
name.text = [receivedict objectForKey:#"Name"];
id.text =[receivedict objectForKey:#"Id"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSString *)senderId
{
return [receivedict objectForKey:#"Id"];
}
- (NSString *)senderDisplayName
{
return [receivedict objectForKey:#"Name"];
}
- (NSDate *)date
{
return 18/03/2016;
}
- (void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date
{
[JSQSystemSoundPlayer jsq_playMessageSentSound];
JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId
senderDisplayName:senderDisplayName
date:date
text:text];
// [demoData.messages addObject:message];
[self finishSendingMessageAnimated:YES];
NSLog(#"%#",message);
}
- (IBAction)cancelbtn:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#end
There is lots of problem in your code .. You are not implementing all JSQ methods .. kindly check how to integrate JSQMessageViewController and than for bubbles that is you main question check answer below :-
You have to use JSQMessagesBubbleImage class to get bubbles like ...
in .h file define
#property (strong, nonatomic) JSQMessagesBubbleImage *outgoingBubbleImageData;
#property (strong, nonatomic) JSQMessagesBubbleImage *incomingBubbleImageData;
in .m file in viewDidLoad
JSQMessagesBubbleImageFactory *bubbleFactory = [[JSQMessagesBubbleImageFactory alloc] init];
self.outgoingBubbleImageData = [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleBlueColor]];
self.incomingBubbleImageData = [bubbleFactory incomingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleLightGrayColor]];
than provide JSQMessages CollectionView DataSource
- (id<JSQMessageBubbleImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [messages objectAtIndex:indexPath.item];
if ([message.senderId isEqualToString:self.senderId]) {
return self.outgoingBubbleImageData;
}
return self.incomingBubbleImageData;
}
I tried after adding the bellow methods in my coding then I have got the bubble with the bellow post other functions - (void)didPressSendButton:(UIButton *)button etc.
- (id<JSQMessageBubbleImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [fularray objectAtIndex:indexPath.item];
if ([message.senderId isEqualToString:self.senderId]) {
return self.outgoingBubbleImageData;
}
return self.incomingBubbleImageData;
}
- (id<JSQMessageAvatarImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
return [JSQMessagesAvatarImageFactory avatarImageWithUserInitials:#"JL" backgroundColor:[UIColor blueColor] textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:12.0] diameter:30.0];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [fularray count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
return fularray[indexPath.row];
}

UITableView section header clickable Delegate not Call

I am new to ios development. I am create app in Storyboard, Initially UItableviewController with one prototype cells, with button and labels. The button has IBAction method in it UItableViewCell Class, the class has Delegate to UItableviewController,the delegate method does not called. But the Images in section view is change. The i post my full code here.
Download whole project from Link
ContactHeaderViewCell.h
#protocol SectionClick;
#protocol SectionClick <NSObject>
#optional
- (void) TickCheckbox : (NSInteger) section;
- (void) UnTickCheckbox : (NSInteger) section;
#end
#interface ContactHeaderViewCell : UITableViewCell
{
id<SectionClick> HeaderDelegate;
}
#property (strong, nonatomic) IBOutlet UILabel *lblName;
#property (strong, nonatomic) IBOutlet UIButton *btnCheckBox;
#property (strong, nonatomic) IBOutlet UIButton *btnArrow;
- (IBAction)btnCheckBox_click:(id)sender;
- (IBAction)btnArrow_Click:(id)sender;
#property (nonatomic, strong) id<SectionClick> HeaderDelegate;
#property (nonatomic) NSInteger section;
- (void) setDelegate:(id)delegate;
#end'
ContactHeaderViewCell.m
#implementation ContactHeaderViewCell
#synthesize HeaderDelegate,section;
- (void) setDelegate:(id)delegate
{
self.HeaderDelegate = delegate;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)btnCheckBox_click:(id)sender {
self.btnCheckBox.selected = !self.btnCheckBox.selected;
if (self.btnCheckBox.selected)
{
if ([self.HeaderDelegate respondsToSelector:#selector(UnTickCheckbox:)])
{
[self.HeaderDelegate UnTickCheckbox:self.section];
}
} else
{
if ([self.HeaderDelegate respondsToSelector:#selector(TickCheckbox:)])
{
[self.HeaderDelegate TickCheckbox:self.section];
}
}
}
ContactTableViewController.m
#import "ContactDetail.h"
#import "ContactHeaderViewCell.h"
#interface ContactTableViewController : UITableViewController<SectionClick>
#property(nonatomic) ContactDetail *contacts;
#property(nonatomic) NSMutableArray *ContactList;
#end
ContactTableViewController.m
#import "ContactTableViewController.h"
#import <AddressBook/AddressBook.h>
#import "ContactHeaderViewCell.h"
#import "UserDetailViewCell.h"
#interface ContactTableViewController ()
#end
#implementation ContactTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[self ArrayContactFunc];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.ContactList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
self.contacts =(self.ContactList)[section];
return (self.contacts.Isopen) ? [self.contacts.mobileNumbers count] : 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *HeaderIdentifier = #"HeaderCell";
self.contacts = (self.ContactList)[section];
ContactHeaderViewCell *HeaderView = (ContactHeaderViewCell *)[self.tableView dequeueReusableCellWithIdentifier:HeaderIdentifier];
if (HeaderView == nil){
[NSException raise:#"headerView == nil.." format:#"No cells with matching CellIdentifier loaded from your storyboard"];
}
HeaderView.lblName.text = self.contacts.fullName;
if(self.contacts.IsChecked)
{
[HeaderView.btnCheckBox setImage:[UIImage imageNamed:#"Unchecked.png"] forState:UIControlStateSelected];
}
else
{
[HeaderView.btnCheckBox setImage:[UIImage imageNamed:#"Checked.png"] forState:UIControlStateSelected];
}
return HeaderView;
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.contacts = (self.ContactList)[indexPath.section];
static NSString *DetailCellIdentifier = #"DetailCell";
UserDetailViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if(!cell)
{
[NSException raise:#"headerView == nil.." format:#"No cells with matching CellIdentifier loaded from your storyboard"];
}
cell.lblDetail.text = (self.contacts.mobileNumbers)[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60.0;
}
-(void)UnTickCheckbox:(NSInteger)section
{
// self.contacts = (self.ContactList)[section];
// self.contacts.IsChecked = NO;
// [self.tableView reloadData];
}
-(void)TickCheckbox:(NSInteger)section
{
// self.contacts = (self.ContactList)[section];
// self.contacts.IsChecked = YES;
// [self.tableView reloadData];
}
Thank in advance.
You must add this line
[HeaderView setHeaderDelegate:self];
in method:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

No visible #interface for 'WHMailActivity' declares the selector 'activityDidFinish:'

my error it No visible #interface for 'WHMailActivity' declares the selector 'activityDidFinish:'
I get a one night but I did not succeed. . . Why are so used elsewhere but I can not. . .
Which I hope to help me. . .
Thanks in advance !
this it my .h
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>
#import "WHMailActivityItem.h"
#interface WHMailActivity : UIViewController
#end
and this it my .m
#import "WHMailActivity.h"
#interface WHMailActivity() <MFMailComposeViewControllerDelegate>
#property (nonatomic, strong)WHMailActivityItem *activityItem;
#end
#implementation WHMailActivity
#pragma mark - UIActivity Overrides
- (NSString *)activityType {
return NSStringFromClass([self class]);
}
- (NSString *)activityTitle {
return NSLocalizedString(#"Mail", #"title for Mail activity item");
}
- (UIImage *)activityImage {
return [UIImage imageNamed:#"mailActivity.png"];
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
if (![MFMailComposeViewController canSendMail]) {
return NO;
}
for (id item in activityItems) {
if ([item isKindOfClass:[WHMailActivityItem class]]) {
return YES;
}
}
return NO;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems {
for (id item in activityItems) {
if ([item isKindOfClass:[WHMailActivityItem class]]){
self.activityItem = item;
}
}
}
- (UIViewController *)activityViewController {
MFMailComposeViewController *composeController = [[MFMailComposeViewController alloc] init];
if (self.activityItem.onMailActivitySelected) {
self.activityItem.onMailActivitySelected(composeController);
}
composeController.mailComposeDelegate = self;
return composeController;
[self performSelector:#selector(mailComposeController)];
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self activityDidFinish:result == MFMailComposeResultSent];
}
#end
In your .h file you have:
#interface WHMailActivity : UIViewController
but it should be:
#interface WHMailActivity : UIActivity
assuming the class is a custom UIActivity.

How can I popup keyboard without UITextField

I want a numeric keyboard without any textfield, I want to press a button and the keyboard will pop, is there an easy way to do it?
Or I need to build my own keyboard?
Thanks. :)
//
// EditingView.h
// TextEditing
//
// Created by Jeffrey Sambells on 10-04-21.
// Copyright 2010 TropicalPixels. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface UIKeyInputExampleView : UIView <UIKeyInput> {
NSMutableString *textStore;
}
#property (nonatomic, retain) NSMutableString *textStore;
#end
//
// EditingView.m
// TextEditing
//
// Created by Jeffrey Sambells on 10-04-21.
// Copyright 2010 TropicalPixels. All rights reserved.
//
#import "UIKeyInputExampleView.h"
#implementation UIKeyInputExampleView
#synthesize textStore;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.textStore = [NSMutableString string];
[self.textStore appendString:#"Touch screen to edit."];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)dealloc {
[textStore dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder { return YES; }
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[self becomeFirstResponder];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
UIRectFrame(rect);
[self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:#"Helvetica" size:24.0f]];
}
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
if (textStore.length > 0) {
return YES;
}
return NO;
}
- (void)insertText:(NSString *)theText {
[self.textStore appendString:theText];
[self setNeedsDisplay];
}
- (void)deleteBackward {
NSRange theRange = NSMakeRange(self.textStore.length-1, 1);
[self.textStore deleteCharactersInRange:theRange];
[self setNeedsDisplay];
}
#end
This code may help you...
You need to implement UIKeyInput protocol. Reference

Objects do not get added to my NSMutableArray

I want to display a list of peoples in an UITableView (I write the name in the ViewController and display them as a list in the UITableViewController).
I just need to stock up the data. But my code add just one object in the NSMutableArray.
Is it because I use a singleton in my class "Customers"?
This is my code so far:
Customers.h
#import <Foundation/Foundation.h>
#interface Customers : NSObject
{
NSString *name;
float age;
}
- (id)initWithName:(NSString *)aname age:(float)aage ;
- (NSString*) name;
- (float) age;
- (void) setName:(NSString*) newName;
- (void) setAge:(float) newAge;
+(Customers*)instance;
#end
Customers.m
#import "Customers.h"
#implementation Customers
- (id)initWithName:(NSString *)aname age:(float)aage {
if ((self = [super init]))
{
self.name = aname;
age = aage;
}
return self;
}
- (NSString*) name{
return name;
}
- (float) age{
return age;
}
- (void) setName:(NSString*) newName
{
name = newName;
}
- (void) setAge:(float) newAge{
age = newAge;
}
+(Customers*)instance{
static dispatch_once_t once;
static Customers *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] initWithName:#"jean" age:24];
});
return sharedInstance;
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#class Customers;
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *nameLabel;
#property (strong, nonatomic) IBOutlet UITextField *ageLabel;
- (IBAction)goToTableView:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "Customers.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)goToTableView:(id)sender {
[[Customers instance] setName:_nameLabel.text];
}
#end
TableViewController.h
#import <UIKit/UIKit.h>
#class Customers;
#interface TableViewController : UITableViewController
{
NSMutableArray *peopleListe;
}
#end
TableViewController.m
#import "TableViewController.h"
#import "Customers.h"
#interface TableViewController ()
#end
#implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Customers *koko = [[Customers alloc ] initWithName:[[Customers instance]name] age:[[Customers instance]age]];
peopleListe = [NSMutableArray arrayWithObjects: nil];
[peopleListe addObject: koko];
NSLog(#"%#",peopleListe);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return peopleListe.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:#"MyBasicCell"];
Customers *list = [peopleListe objectAtIndex:indexPath.row];
cell.textLabel.text = list.name;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
#end
Thank you for your help in advance.
According to your comment, you want to add an object every time the controller gets loaded.
- (void)viewDidLoad {
[super viewDidLoad];
Customers *koko = [[Customers alloc ] initWithName:[[Customers instance] name]
age:[[Customers instance] age]];
peopleListe = [NSMutableArray arrayWithObjects: nil];
[peopleListe addObject: koko];
NSLog(#"%#",peopleListe);
}
I see multiple problems with this:
I'm not sure viewDidLoad is the correct method to embed this in, i don't think it is guaranteed to get called if the view is already in memory. (I don't have any ios experience, so I'm not sure.) It might even be that your controller gets removed and created from scratch every time you use it.
You replace your whole list when you allocate peopleList. You should check for its existence and only allocate it if it does not exist yet. (Obj-C initializes instance variables as nil, so a simple check is enough.)
Apart from that, you seem to mix controller and model code. If you have the time you should look into moving the array into the model and out of the controller code.

Resources