How to show detail in table view on xcode - objective-c

How can I create 10 rows in a table view on xcode and show a detail as alert when I click on them?

You have to write Logic in didselectRowAtIndexPath Method
arr is NSMutablearray..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Detail"
message:[arr objectAtIndex:indexPath.row]
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}

Related

How to pop up UIAlertView when the numberofrow (TableView) is 1

I have an 'add' button to create new row of tableview. And the number of row is limited to one.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
I'd like the button to pop up an alert view when there is one row, not allowing to create a new row.
However i don't know how to implement an 'if condition' in the Button action as following:
- (IBAction)add
{
if (condition)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Hi" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
}
[alert show];
}
Please Help! Sorry for not being professional, I'm trying to learn objective c
All you have to do is -
- (IBAction)add
{
if ([self.tableView numberOfRowsInSection:0] == 1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Hi" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
}
[alert show];
}
I think you need have an mutableArray to hold the data of UITableView and return the count of array provide to UITableView through "numberOfRowsInSection" method, when you want to add a row , you just add a data into the Array and reload the tableView.
ps:you can't add row but return 1 to UITableView
Create boolean variable shouldCreateRow.
Write the tableview datasource method as
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(shouldCreateRow){
return 1;
}
}
and the action method as
- (IBAction)add:(UIButton *)sender {
sender.selected=!sender.selected;
if (sender.selected && !shouldCreateRow) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"One row created" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
shouldCreateRow=TRUE;
[myTable reloadData];
}else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"Already there is One row." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}

Keeping text in an alert view, and saving text to an NSString?

I have a UITableView that is meant have a list of names. I want it to bring up an UIAlertView whenever a name is clicked, so that the accompanying information to the name (for now just the age) will be allowed to be changed.
As of now, I can get an alertView with a UITextField to appear, however once you close the alertView, the text inside the UITextField resets to the placeholder. How can I get the text to stay with the UIAlertView?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"Set Age"
message:[_tableData1 objectAtIndex:indexPath.row]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = #"18";
[alert show];
}
Also, how can I get the text from the UIAlertView, and save it as a NSString?
Thank you!
You need to set your view controller as the delegate and then retrieve the value in the callback. You also need to add UIAlertViewDelegate to your #interface
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set Age" message:[_tableData1 objectAtIndex:indexPath.row] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = #"18";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex == alertView.cancelButtonIndex) {
return;
}
NSString * yourString = [alertView textFieldAtIndex:0].text
}

popViewController doesn't work with UIAlertView

I am having problem with AlertView. I am trying to use the UIAlertView and after click ok it will return back to the previous screen but it do not seems to work any advice ?
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
[self.navigationController popViewControllerAnimated:YES];
}
or
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iPoly"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
return;
}
both doesn't work
For this purpose you've to use UIAlertView's delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
First use this in your #interface <UIAlertViewDelegate>
Then set the delegate, self.yourAlertView.delegate=self;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//first button which should be the OK button
{
[self.navigationController popViewControllerAnimated:YES];
}
}
use the delegate method of UIAlertView, see the answer given by iNoob. It does not make a sense if you write anything after the "return;" statement as the code below "return;" statement will never get executed.
refer apple developer link for more details on UIAlertView delegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
or a simple tutorial on alert view
http://mobile.tutsplus.com/tutorials/iphone/uialertview/
You just need to implement UIAlerView Delegate Methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
write your code for Pop to previous Controller here.You can the clicked button index and on the basis of that you can use.Don't for Conform UIAlertViewDelegate to Interface.

Multiselect table view controller items for email attachment

Here is my XCode 4.3.2 problem... I have a table displaying multi-select NSArray data in a table controller. I would like to select multiple items in the table, click a button on the bottom of the view controller, and attach the selected documents in the project that are referenced in the table's NSArray to an email message. I have the email portion working ok, but not attaching anything. I also have the multi-select check marks working correctly. Any ideas on getting multi-select attachment accomplished? I assume I have to build a mutableArray in which the table data cell text = the local PDF file name.???
Here is some of my code that I think will have to be edited for building the array?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(#"Items I selected # %d:", indexPath.row);
NSLog(#"Items I selected: %#", selectedIndexes);
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(#"Items I de-selected # %d:", indexPath.row);
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
The mail component:
- (IBAction)sendMailButton:(id)sender {
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Documentation Attached"];
[mailer setMessageBody:#"Attached are documents from staff." isHTML:NO];
//[mailer setMessageBody:emailBody isHTML:NO];
//iPad presentation enhancement:
mailer.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
}

Unable to handle setEditing method through UIBarButtonItem

I have added a bar button item in my tool bar and I implemented its functionality in the method given below. I am not able, however, to display a delete button in each row. How can I do this?
-(void)setEditing:(BOOL)editing
animated:(BOOL)animated
{
[super setEditing:editing
animated:animated];
[editButton setEnabled:!editing];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *selectedFile = (NSString*) [directoryContents objectAtIndex: indexPath.row];
NSString *selectedPath = [directoryPath stringByAppendingPathComponent:selectedFile];
BOOL canWrite = [[NSFileManager defaultManager] isWritableFileAtPath:selectedPath];
if(!canWrite)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"status"
message:#"path isnt writable"
delegate:nil
cancelButtonTitle:#"cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
NSError *err = nil;
if(! [[NSFileManager defaultManager] removeItemAtPath:selectedPath error:&err])
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"status"
message:#"cannot delete"
delegate:nil
cancelButtonTitle:#"cancel"
otherButtonTitles:nil];
[alert1 show];
[alert1 release];
}
// Delete the row from the data source.
NSArray *deletedPaths = [NSArray arrayWithObject: indexPath];
[self loadDirectoryContents];
[self.tableview deleteRowsAtIndexPaths:deletedPaths withRowAnimation:YES];
}
}
The button on the toolbar will need to be connected to an interface action in the custom table view controller subclass. Define the action as follows -
- (IBAction)startEditing {
[self setEditing:YES animated:YES];
}
-(void)setEditing:(BOOL)editing
animated:(BOOL)animated
{
[super setEditing:editing
animated:animated];
[editButton setEnabled:!editing];
}
You will have to implement the delegate method tableView:editingStyleForRowAtIndexPath and return UITableViewCellEditingStyleDelete for the rows you wish to enable it.