I am posting the code which I tried.I am getting string value,but when adding all the values of nsstring to nsmutablearray and printing it ,I get null value.
//ViewController.h
#interface ViewController : UIViewController{
NSString *stringValueVideoiD;
NSMutableArray *TableVideoIDArray;
}
//ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
TableVideoIDArray=[[NSMutableArray alloc]init];
}
-(void) getAllRows{
if(sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT * FROM table"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db,[sqlStatement UTF8String] , -1, &compiledStatement, NULL)== SQLITE_OK)
{
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
char *videoId = (char *) sqlite3_column_text(compiledStatement, 1);
stringValueVideoiD = [[NSString alloc] initWithUTF8String:videoId];
[TableVideoIDArray addObject:stringValueVideoiD];
NSLog(#"vids id:%#",TableVideoIDArray);
//vids id:(null) is printed
}
}
}
}
Check if stringValueVideoiD has some value. Try to print it before addObject. Check you sql statement if it is returning any value.
Related
I am posting the code which I tried.I am getting string value and then I am adding it to an array and getting the array value too.But problem comes when I try to use the array value in other method ,it's value is blank.
//ViewController.h
#interface ViewController : UIViewController{
NSString *stringValueVideoiD;
NSMutableArray *TableVideoIDArray;
}
//ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
TableVideoIDArray=[[NSMutableArray alloc]init];
[self getAllRows];
}
-(void) getAllRows{
if(sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT * FROM table"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db,[sqlStatement UTF8String] , -1, &compiledStatement, NULL)== SQLITE_OK)
{
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
char *videoId = (char *) sqlite3_column_text(compiledStatement, 1);
stringValueVideoiD = [[NSString alloc] initWithUTF8String:videoId];
TableVideoIDArray=[[NSMutableArray alloc]init];
[TableVideoIDArray addObject:stringValueVideoiD];
NSLog(#"vids array:%#",TableVideoIDArray);
//vids aray:value is printed
}
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil) {
cell=[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"here:%#",TableVideoIDArray);
//here comes the problem.My console result is
2013-09-18
11:18:24.431 TubeAlert[468:11303] here:(
)
//so guys I need your help why this is happenning or I am making mistakes in my code.
TableVideoIDArray allocated two times.
In viewDidLoad, use
TableVideoIDArray=[NSMutableArray array];
don't allocate with in a While loop.
#import "AddEandM.h"
#import "Questions.h"
#import "sqlite3.h"
#interface AddEandM ()
#end
#implementation AddEandM
#synthesize Topic, Question, Answer1, Answer2, Answer3, CorrectAnswer, status;
-(IBAction) backgroundTouched:(id)sender {
[Topic resignFirstResponder];
[Question resignFirstResponder];
[Answer1 resignFirstResponder];
[Answer2 resignFirstResponder];
[Answer3 resignFirstResponder];
[CorrectAnswer resignFirstResponder];
}
-(IBAction) textfieldReturn:(id)sender {
[sender resignFirstResponder];
}
- (void) saveData
{
sqlite3 * Questions;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Questions) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
#"INSERT INTO DATABASE (Topic, Question, Answer1, Answer2, Answer3, CorrectAnswer) VALUES (\"%#\", \"%#\", \"%#\", \"%#\", \"%#\", \"%#\")",Topic.text, Question.text, Answer1.text, Answer2.text, Answer3.text, CorrectAnswer.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2 (Questions, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
status.text = #"Question/Answers Added";
Topic.text = #" ";
Question.text = #" ";
Answer1.text = #" ";
Answer2.text = #" ";
Answer3.text = #" ";
CorrectAnswer.text = #" ";
} else {
status.text = #"Failed to add Question/Answers";
}
sqlite3_finalize(statement);
sqlite3_close(Questions);
}
}
-(IBAction)Back:(id)sender {
Questions *second = [[Questions alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;
sqlite3 * Questions;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex: 0];
databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
#"Questions"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Questions) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "Create Table If Not Exists Questions (QuestionNo Integer AutoIncrement, Question Text, Answer1 Text, Answer2 Text, Answer 3 Text, Correct Answer Text)";
if (sqlite3_exec(Questions, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
status.text = #"Failed to Create Table";
}
sqlite3_close(Questions);
} else {
status.text =#"Failed to open/create database";
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am creating a revision application for my computer science class, I have very limited knowledge in regards to programming in Xcode/Ob C, can anyone assist me and tell me what is going wrong here? My database is named "Questions" and contains 2 tables: "EasyandMedium" as well as "Hard", however, I cannot get my program to save data to this database - Please Help!
What exactly is the problem you are having? Do you know if your fileExistsAtPath call succeeds? Does the sqlite3_open call succeed?
There are lots of examples for using SQLite3 in Xcode such as I am having trouble linking my XCode Project with my SQLite3 database. I would suggest checking one out as they make a great starting point.
I have a UITable, and I like to add an image in the detail view for a cell. I can handle the selection for any image from the camera roll or from the camera:
cell.imageView.image = someImage;
But how do I define a specific image - in the case above: "someImage", so that the next time the app is run, the correct image is shown for each item.
UPDATE. This is the code I'm using to snap/select an image..
- (IBAction)btnTakePicture_Clicked:(id)sender
{
NSLog(#"%s", __FUNCTION__);
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..." delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Image Gallary", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%s", __FUNCTION__);
switch (actionSheet.tag)
{
case 1:
switch (buttonIndex)
{
case 0:
{
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
//picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
#endif
}
break;
case 1:
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
break;
}
break;
default:
break;
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSLog(#"%s", __FUNCTION__);
dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
imgPicture.image = [[UIImage alloc] initWithData:dataImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
UPDATE 2. With the help from people below, I think this solution will work for me:
- (IBAction)photoLibraryAction:(id)sender
{
int c = self.capturedImages.count;
for (int i=0; i < c; i++ ){
if (self.imageView.tag == cellTag) {
NSLog(#"found it");
} else {
NSLog(#"can't find it");
}
}
}
if ([self.capturedImages count] == 1)
{
// we took a single shot
[self.imageView setImage:[self.capturedImages objectAtIndex:0]];
[self.imageView setTag:myTag];
}
I would do in this way.
#interface CustomClassCell : UITableViewCell
#property (unsafe_unretained, nonatomic) IBOutlet UIImageView *HomePicture;
#end
#interface CustomClass : NSObject
#property (nonatomic, copy) NSString *PicPath;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomClassCellIdentifer"];
id object = [self.tableData objectAtIndex:indexPath.row];
CustomClass *myObject = (CustomClass*)object;
cell.HomePicture.image = [UIImage imageNamed:myObject.PicPath];
return cell;
}
I have a requirement to adding a path in my MKMapview between two annotation pin. There is no issue with ios 5 or older but when i try to run app in ios 6 app is surprisingly quit. below is my code. If there is any correction in my code then please suggest me.
1.So my question is suggest the best way how to decrees memory in my following code so that i can solve crash in my Application.
- (void)viewDidLoad
{
NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoints:pathArray mapView:_mapView];
[_mapView addAnnotation:annotation];
[annotation release];
[pathArray removeAllObjects];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"%s",__FUNCTION__);
if ([annotation isKindOfClass:[NVPolylineAnnotation class]])
{
NSLog(#"=========ANOTATION=========NVPolylineAnnotationView START");
//ann=[ann initWithAnnotation:annotation mapView:_mapView];
NVPolylineAnnotationView *ann=[[NVPolylineAnnotationView alloc] init];
return [[ann initWithAnnotation:annotation mapView:_mapView] autorelease];//[[[NVPolylineAnnotationView alloc] initWithAnnotation:annotation mapView:_mapView] autorelease];
}
else if([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pointers"] ;
// annView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annView.animatesDrop=NO;
annView.canShowCallout = TRUE;
return [annView autorelease];
}
else if([annotation isKindOfClass:[PlacePin class]])
{
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
}
return nil;
}
- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
NSLog(#"%s",__FUNCTION__);
// fixes that some marker are behind the polyline
for (int i=0; i<[views count]; i++)
{
MKAnnotationView *view = [views objectAtIndex:i];
if ([view isKindOfClass:[NVPolylineAnnotationView class]])
{
[[view superview] sendSubviewToBack:view];
/* In iOS version above 4.0 we need to update the polyline view after it
has been added to the mapview and it ready to be displayed. */
NSString *reqSysVer = #"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
[self updatePolylineAnnotationView];
}
}
}
NSLog(#"----------didAddAnnotationViews");
}
- (void)updatePolylineAnnotationView
{
NSLog(#"%s",__FUNCTION__);
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
[mapView selectAnnotation:mp animated:NO];
*/
for (NSObject *a in [_mapView annotations])
{
if ([a isKindOfClass:[NVPolylineAnnotation class]])
{
NVPolylineAnnotation *polyline = (NVPolylineAnnotation *)a;
NSObject *pv = (NSObject *)[_mapView viewForAnnotation:polyline];
if ([pv isKindOfClass:[NVPolylineAnnotationView class]])
{
NVPolylineAnnotationView *polylineView =
(NVPolylineAnnotationView *)[_mapView viewForAnnotation:polyline];
[polylineView regionChanged];
}
}
}
}
I need help to make an Array, in xcode, to collect 25 existing buttons in a Window(NView) for, for example, change background colors or titles of others buttons when one button is pressed.
Thanks.
This is the Code:
Botones.h
#import <Cocoa/Cocoa.h>
extern const NSString* Nivel[];
extern const NSString* Ayuda[];
extern const NSString* Boleo[];
BOOL Estado[5][5];
int Columna;
int Fila;
//NSColor* Rojo = (NSColor *)redColor;
#interface Botones : NSObject {
IBOutlet id Intents;
IBOutlet id Nivel;
IBOutlet id BoxBot;
NSArray *Arbot;
}
- (IBAction)OrBoton:(id)sender;
Botones.m
#import "Botones.h"
const NSString *Nivel[] = {#"101214",#"0002040507091517192202224",#"01030506080910111314151618192123"};
BOOL Estado[5][5] = {FALSE};
int Columna = 0;
int Fila = 0;
#implementation Botones
- (void)awakeFromNib {
Arbot = [[BoxBot subviews] copy];
}
- (IBAction)OrBoton:(id)sender {
[Intents setIntValue:[sender tag]];
Columna =[sender tag] % 5;
Fila = [sender tag] / 5;
if (Estado[Columna][Fila] == FALSE) {
Estado[Columna][Fila] = TRUE;
[sender setTitle:#"OK"];
//[sender setBackgroundColor:[NSColor redColor]];
}
else {
Estado[Columna][Fila] = FALSE;
[sender setTitle:#""];
//[sender setBackgroundColor:[NSColor greenColor]];
}
if (Fila > 0) {
if (Estado[Columna][Fila - 1] == FALSE) {
Estado[Columna][Fila - 1] = TRUE;
NSLog(#"%#",[BoxBot subviews]);
[[Arbot objectAtIndex:5] setTitle:#"OK"];// <-- Don't changes button's title
}
}
}
If the buttons are the only subviews in the view, you can use this:
//view is a variable pointing to the view containing the buttons
NSArray *buttons = [[view subviews] copy];
If there are other views in the view, you will have to filter them out:
//view is a variable pointing to the view containing the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:25];
for(NSView *v in [view subviews]) {
if([v isKindOfClass:[NSButton class]]) [buttons addObject:v];
}
Edit
...
#interface Botones : NSObject {
IBOutlet id Intents;
IBOutlet id Nivel;
IBOutlet id BoxBot;
NSArray *ArBot;
}
...
Botones.m
...
#implementation Botones
- (void)awakeFromNib {
ArBot = [[BoxBot subviews] copy];
/*or
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:25];
for(NSView *v in [BoxBot subviews]) {
if([v isKindOfClass:[NSButton class]]) [array addObject:v];
}
ArBot = [array copy];
[array release];*/
}
...