سؤال

أحاول أن أحصل على عرض مشغول خلال عملية البحث ، لكن يبدو أنه لا يبدو أنه لا يعرضه أبدًا.

ما أحاول تحقيقه

  1. ينقر المستخدم على زر البحث بمجرد إدخال النص في uisearchbar.
  2. يتم تفويض النص الذي تم إدخاله إلى SearchBarsearchButtonclicked.
  3. أظهر "عرض مشغول" وهو عبارة عن UIView التي تحتوي على uiActivityIndicatorView للإشارة إلى أن البحث جاري.
  4. إجراء بحث يتواصل مع موقع ويب.
  5. عند الانتهاء من البحث ، قم بإزالة "العرض المزدحم".

ما هو الخطأ

تعمل عملية البحث بشكل جيد ، باستخدام مصحح الأخطاء ، يمكنني رؤيتها تتحرك من خلال وظيفة SearchBarsearchButtonclicked بشكل جيد ، لكن "العرض المزدحم" لا يظهر أبدًا. يستغرق البحث من 2 إلى 3 ثوانٍ ، لذا من الناحية النظرية ، يجب أن أرى "وجهة نظري المزدحمة" تظهر لتلك الثواني 2-3.


محاولة الكود 1 - أضف وإزالة "عرض مشغول" من الرؤية الإشراف **

- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar {

 [activeSearchBar setShowsCancelButton:NO animated:YES];
 [activeSearchBar resignFirstResponder];
 [busyView activateSpinner]; //start the spinner spinning
 [self.view addSubview:busyView]; //show the BusyView

 Search *search = [[Search alloc]init];
 results = [search doSearch:activeSearchBar.text];

 [busyView deactivateSpinner]; //Stop the spinner spinning
 [busyView removeFromSuperview]; //remove the BusyView
 [search release]; search = nil;

}

نتائج المحاولة 1 - لا يظهر ، ومع ذلك ، إذا قمت بالتعليق على removeFromSuperView ، تبقى طريقة العرض المزدحمة على الشاشة.


محاولة الكود 2 - باستخدام الرسوم المتحركة

- (void)searchBarSearchButtonClicked:(UISearchBar *)activeSearchBar {



[activeSearchBar setShowsCancelButton:NO animated:YES]; 
 [activeSearchBar resignFirstResponder];

 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0];
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDidStopSelector:@selector(animationDidStop)];

 [busyView activateSpinner]; //start spinning the spinner
 [self.view addSubview:busyView]; //show the busy view

 [UIView commitAnimations];

 Search *search = [[Search alloc]init];
 results = [search doSearch:activeSearchBar.text];


 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0];
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDidStopSelector:@selector(animationDidStop)];

 [busyView deactivateSpinner]; //sets stops the spinner spinning
 [busyView removeFromSuperview]; //remove the view

 [UIView commitAnimations];
 [search release]; search = nil;
}

نتائج المحاولة 2 - لم ير ظهور "عرض مشغول" يظهر

هل كانت مفيدة؟

المحلول

حسنًا ، هذا هو الحل:

  • إنشاء Singleton Class MyLoadingView. يجب أن تحتوي هذه الفئة على إظهار وإخفاء طرق ثابتة.
  • في MyLoadingView Constructor ، يجب عليك إنشاء طريقة عرض يدويًا بخلفية سوداء شبه شفافة و ActivityView بداخلها. ضع هذا المنظر في [[UIApplication sharedApplication] keyWindow]. إخفائه.
  • [MyLoadingView show] الاحتجاج فقط يجلب MyLoadingView كائن إلى مقدمة في الحاوية: [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:myLoadingViewSharedInstance];. لا تنس أيضًا بدء تشغيل الرسوم المتحركة ActivityView.
  • [MyLoadingView hide] يوقف ActivityView الرسوم المتحركة واختباء المشاركة.

يرجى ملاحظة أنك بحاجة إلى الاحتجاج [MyLoadingView show] في موضوع منفصل.

نصائح أخرى

تسري تغييرات واجهة المستخدم في الكاكاو فقط في المرة التالية التي يعيد فيها الكود التحكم إلى حلقة التشغيل. طالما أن مهمة البحث الخاصة بك تعمل بشكل متزامن على الموضوع الرئيسي ، فسيحظر التنفيذ ، بما في ذلك تحديثات واجهة المستخدم.

يجب عليك تنفيذ المهمة المستهلكة للوقت بشكل غير متزامن في الخلفية. هناك عدد من الخيارات لهذا (NSOperation, performSelectorInBackground:..., ، الإرسال الكبرى الكبرى ، ...).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top